In [1]:
using DataFrames
using TensorFlow
using Images
In [2]:
data_set_folder = "/raided/datasets/sketches_png/png/"
list_of_imgs = open(data_set_folder * "filelist.txt")
img_paths = readlines(list_of_imgs)
close(list_of_imgs)
In [3]:
train_paths = Array(String, 700)
path_index = 1
count = 1
for i = 1:700
train_paths[i] = img_paths[path_index]
path_index += 1
count += 1
if count % 71 == 0
count = 1
path_index += 10
end
end
test_paths = Array(String, 100)
path_index = 71
count = 1
for i = 1:100
test_paths[i] = img_paths[path_index]
path_index += 1
count += 1
if count % 11 == 0
path_index += 70
count = 1
end
end
In [4]:
files_in_folder = readdir(data_set_folder)
labels = filter(x->isdir(data_set_folder * x) == true, files_in_folder)
Out[4]:
In [5]:
x = TensorFlow.placeholder(Float32)
y_ = TensorFlow.placeholder(Float32)
W = get_variable("weights", [77841, 10], Float32)
b = get_variable("bias", [10], Float32)
Out[5]:
In [6]:
y = nn.softmax(x*W + b)
cross_entropy = reduce_mean(-reduce_sum(y_ .* log(y), reduction_indices=[2]))
train_step = train.minimize(train.GradientDescentOptimizer(.00001), cross_entropy)
Out[6]:
In [7]:
type DataLoader
cur_id::Int
order::Vector{Int}
end
TrainLoader() = DataLoader(1, shuffle(1:700))
TestLoader() = DataLoader(1, shuffle(1:100))
Out[7]:
In [8]:
test_image = load("/raided/datasets/sketches_png/png/airplane/1.png")
println(size(test_image))
something = reshape(test_image, 1234321)
black_pixels = filter(x-> x.val!=1, something)
white_pixels = filter(x-> x.val==1, something)
println(length(black_pixels))
println(length(white_pixels))
In [9]:
smaller_image = restrict(restrict(test_image))
println(size(smaller_image))
test_image = reshape(smaller_image, 77841)
black_pixels = filter(x-> x.val!=1, test_image)
white_pixels = filter(x-> x.val==1, test_image)
println(length(black_pixels))
println(length(white_pixels))
asdf = colorim(smaller_image)
Out[9]:
In [10]:
even_smaller_image = restrict(smaller_image)
println(size(even_smaller_image))
test = reshape(even_smaller_image, 19600)
black_pixels = filter(x-> x.val!=1, test)
white_pixels = filter(x-> x.val==1, test)
println(length(black_pixels))
println(length(white_pixels))
asdf = colorim(even_smaller_image)
Out[10]:
In [11]:
function next_batch(loader::DataLoader, batch_size)
x = zeros(Float32, batch_size, 77841)
y = zeros(Float32, batch_size, 10)
for i = 1:batch_size
path = train_paths[loader.order[loader.cur_id]][1:end-1]
sketch_name = split(path, "/")[1]
img = load(data_set_folder * path)
img = restrict(restrict(img))
flatten_img = reshape(img, 77841)
x[i, :] = flatten_img
label = find((x -> x == sketch_name), labels)[1]
y[i, label] = 1.0
loader.cur_id += 1
if loader.cur_id > 700
break
end
end
x, y
end
Out[11]:
In [12]:
function get_test(loader::DataLoader)
x = zeros(Float32, 100, 77841)
y = zeros(Float32, 100, 10)
for i = 1:100
path = test_paths[loader.order[loader.cur_id]][1:end-1]
sketch_name = split(path, "/")[1]
img = load(data_set_folder * path)
img = restrict(restrict(img))
flatten_img = reshape(img, 77841)
x[i, :] = flatten_img
label = find((x -> x == sketch_name), labels)[1]
println("--------------------------")
println(label)
println(loader.cur_id)
y[i, label] = 1.0
loader.cur_id += 1
if loader.cur_id > 100
println("========================")
println(sketch_name)
break
end
end
x, y
end
Out[12]:
In [13]:
loader = TrainLoader()
saver = train.Saver()
gpu_options = TensorFlow.tensorflow.GPUOptions(allow_growth=true, per_process_gpu_memory_fraction=0.4)
config = TensorFlow.tensorflow.ConfigProto(gpu_options=gpu_options)
sess = Session(config=config)
run(sess, initialize_all_variables())
for i in 1:70
batch = next_batch(loader, 10)
run(sess, train_step, Dict(x=>batch[1], y_=>batch[2]))
println("DONE BATCH ", i)
end
In [14]:
save_path = mktempdir()
println(save_path)
train.save(saver, sess, joinpath(save_path,"test"))
In [15]:
correct_prediction = indmax(y, 2) .== indmax(y_, 2)
accuracy=reduce_mean(cast(correct_prediction, Float64))
test_loader = TestLoader()
testx, testy = get_test(test_loader)
Out[15]:
In [16]:
println(run(sess, indmax(y, 2), Dict(x=>testx, y_=>testy)))
println(run(sess, indmax(y_, 2), Dict(x=>testx, y_=>testy)))
println("\n \n Accuracy: ", run(sess, accuracy, Dict(x=>testx, y_=>testy)))
In [17]:
function sketch_input(path, sketch_name)
x = zeros(Float32, 1, 77841)
y = zeros(Float32, 1, 10)
img = load(path)
img = restrict(restrict(img))
flatten_img = reshape(img, 77841)
x[1, :] = flatten_img
label = find((x -> x == sketch_name), labels)[1]
y[1, label] = 1.0
x, y
end
Out[17]:
In [18]:
sketch_path = "/raided/datasets/sketches_png/png/airplane/73.png"
sketch_label = "airplane"
test_img, test_label = sketch_input(sketch_path, sketch_label)
what_it_thinks = labels[run(sess, indmax(y,2), Dict(x=>test_img, y_=>test_label))[1] + 1]
println("Is it an " * what_it_thinks * "? Answer: ", (sketch_label == what_it_thinks), ".")
In [ ]: