Using RGB improves accuracy
import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import glob
from pathlib import Path
%matplotlib inline
file_path = "datasets/cifar-10-batches-bin/data_batch_*"
train_bins = glob.glob(file_path)
def cifar_dataset(files_list:list) -> tf.data.Dataset:
data = tf.data.FixedLengthRecordDataset(files_list,1+3*32*32)
data = data.map(lambda x: tf.decode_raw(x,tf.uint8),num_parallel_calls=4)
data = data.map(lambda x: (x[1:],tf.expand_dims(x[0],0)),num_parallel_calls=4)
data = data.map(lambda x,y: (tf.reshape(x,(3,32,32)),y),num_parallel_calls=4)
data = data.map(lambda x,y: (tf.transpose(x,(1,2,0)),y),num_parallel_calls=4)
data = data.map(lambda x,y: (tf.image.convert_image_dtype(x,tf.float32),y),num_parallel_calls=4)
return data
with tf.device('/cpu:0'):
train_dataset = cifar_dataset(train_bins)
train_dataset = train_dataset.shuffle(20000)
train_dataset = train_dataset.repeat(40)
train_dataset = train_dataset.batch(10)
train_dataset = train_dataset.prefetch(2)
train_iterator = train_dataset.make_one_shot_iterator()
train_dataset.output_shapes, train_dataset.output_types
Check input (Restart kernel and dont call this check before training)
im,l = train_iterator.get_next()
with tf.Session() as sess:
imr,lr = sess.run([im,l])
print(imr.shape)
print(lr.transpose())
plt.imshow(imr[0])
saver = tf.train.import_meta_graph('models/WT_TRANSFER/wt_transfer_eg.chpt.meta')
features = tf.get_collection('features')[0]
handle = tf.get_collection('handle')[0]
features,handle
Check loading
NOTE: We do not have to restore model here, Since we had encoded the weights in the initializer
with tf.Session() as sess:
#We do not have to restore model here, Since we had encoded the weights in the initializer
sess.run(tf.global_variables_initializer())
#Check if the right weights have been loaded
print(sess.run(tf.get_collection('variables',scope='Conv2/bias')))
hdl = sess.run(train_iterator.string_handle())
out = sess.run(features,{handle:hdl})
out.shape
classifier = tf.layers.dense(features,10,name="Dense_10")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(tf.get_collection('variables',scope='Conv2/bias')))
hdl = sess.run(train_iterator.string_handle())
print(sess.run(classifier,{handle:hdl}))
label = tf.get_collection('target')[0]
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=tf.one_hot(label,10),logits=classifier))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
IMPORTANT: Use a new saver and dont mix up with meta graph saver. It wont contain classifier and other tensors created in this graph
!rm models/CIFAR10_rbg/*
#We need a new saver as we have created new variables
saver = tf.train.Saver()
import time
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
hdl = sess.run(train_iterator.string_handle())
start = time.time()
try:
i = 1
tmp = []
while True:
i = i+1
l,_ = sess.run([loss,train],{handle:hdl})
tmp.append(l)
if i%5000 == 0:
avg_loss = np.array(tmp).mean()
print("Batch: ",i,avg_loss)
tmp = []
except tf.errors.OutOfRangeError:
pass
end = time.time()
elapsed = end-start
print("Elapsed time : ", elapsed, " s")
saver.save(sess,'models/CIFAR10_rbg/cifar_model.ckpt')
with tf.device('/cpu:0'):
test_dataset = cifar_dataset(['datasets/cifar-10-batches-bin/test_batch.bin'])
test_dataset = test_dataset.batch(10)
test_dataset = test_dataset.prefetch(2)
test_iterator = test_dataset.make_one_shot_iterator()
def get_accuracy(predict:'eg: [2,4,1,...]',true: 'eg: [2,4,1,...]') -> int:
correct_pred = tf.equal(predict,true)
#We have to cast [True,False,True,...] --> [1,0,1...]
acc = tf.reduce_mean(tf.cast(correct_pred,tf.float32))
return acc
with tf.Session() as sess:
saver.restore(sess,'models/CIFAR10_rbg/cifar_model.ckpt')
hdl = sess.run(test_iterator.string_handle())
#IMPORTANT:
#Dont place this code inside the loop! This will slow down everything
acc = get_accuracy(tf.argmax(classifier,axis=1),tf.transpose(tf.argmax(tf.one_hot(label,10),axis=2)))
try:
i = 0
acc_list = []
while True:
i = i+1
a = sess.run(acc,{handle:hdl})
acc_list.append(a)
if i%100 == 0:
print(i, "Mean Acc : ", np.array(acc_list).mean())
acc_list = []
except tf.errors.OutOfRangeError:
pass
Image.open("pics/cifar22.png") #ship - 8
Image.open("pics/cifar3.png") #automobile - 1
Image.open("pics/cifar24.png") #automobile - 1
test_ims_paths = tf.placeholder(tf.string)
serving_data = tf.data.Dataset.from_tensor_slices(test_ims_paths)
def read_img(filepath):
image_string = tf.read_file(filepath)
image = tf.image.decode_png(image_string)
image = tf.image.convert_image_dtype(image,tf.float32)
#image = tf.image.resize_images(image,(32,32))
return image
serving_data = serving_data.map(lambda x: read_img(x))
serving_data = serving_data.map(lambda x: (x,tf.expand_dims(tf.cast(0,tf.uint8),axis=0)))#To match with iterator
serving_data = serving_data.batch(5)
NOTICE: The tensor shape None matches with the original 32,32
serving_data.output_shapes,serving_data.output_types
serving_iterator = serving_data.make_initializable_iterator()
with tf.Session() as sess:
saver.restore(sess,'models/CIFAR10_rbg/cifar_model.ckpt')
ims_paths = ["pics/cifar22.png","pics/cifar3.png","pics/cifar24.png"]
hdl,_ = sess.run([serving_iterator.string_handle(),serving_iterator.initializer],{test_ims_paths:ims_paths})
predictions = tf.argmax(classifier,axis=1)
out = sess.run(predictions,{handle:hdl})
print(out)
1) Restore checkpoint only when you want to use weights in the checkpoint. In this example, we had included the weights as a part of initializers. Hence we did not have to restore chpt before training!
2) Create a new saver to save trained model and dont mess up with the meta graph import saver
3) Do not call the get_next of child iterators when using feedable iterators
4) Using RGB imporves Accuracy