In [1]:
using DataFrames
using TensorFlow
using Distributions
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)[1:400]
close(list_of_imgs)
In [3]:
train_paths = Array(String, 350)
count = 1
for i = 1:350
train_paths[i] = img_paths[count]
count += 1
if count % 71 == 0
count += 10
end
end
test_paths = Array(String, 50)
count = 71
for i = 1:50
test_paths[i] = img_paths[count]
count += 1
if count % 81 == 0
count += 70
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, 5], Float32)
b = get_variable("bias", [5], 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:350))
TestLoader() = DataLoader(1, shuffle(1:50))
Out[7]:
In [8]:
function next_batch(loader::DataLoader, batch_size)
x = zeros(Float32, batch_size, 77841)
y = zeros(Float32, batch_size, 5)
for i = 1:batch_size
path = train_paths[loader.order[loader.cur_id]][1:end-1]
println(path)
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 > 350
loader.cur_id = 1
break
end
end
x, y
end
Out[8]:
In [9]:
function get_test(loader::DataLoader)
x = zeros(Float32, 50, 77841)
y = zeros(Float32, 50, 5)
for i = 1:50
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 > 50
println("========================")
println(sketch_name)
break
end
end
x, y
end
Out[9]:
In [10]:
loader = TrainLoader()
sess = Session()
run(sess, initialize_all_variables())
for i in 1:35
batch = next_batch(loader, 10)
run(sess, train_step, Dict(x=>batch[1], y_=>batch[2]))
println("DONE BATCH ", i)
end
In [11]:
correct_prediction = indmax(y, 2) .== indmax(y_, 2)
accuracy=reduce_mean(cast(correct_prediction, Float64))
test_loader = TestLoader()
testx, testy = get_test(test_loader)
println(run(sess, indmax(y, 2), Dict(x=>testx, y_=>testy)))
println(run(sess, indmax(y_, 2), Dict(x=>testx, y_=>testy)))
println(run(sess, accuracy, Dict(x=>testx, y_=>testy)))
In [12]:
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[12]:
In [13]:
sketch_path = "/raided/datasets/sketches_png/png/airplane/79.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 [ ]: