def main(path): cars, noncars, data_info = load_training_data(path) ind = np.random.randint(0, len(cars)) name = cars[ind] img = cv2.imread(name) # Select a small fraction of pixels to plot by subsampling it scale = max(img.shape[0], img.shape[1], 64) / 64 # at most 64 rows and columns img_small = cv2.resize(img, (np.int( img.shape[1] / scale), np.int(img.shape[0] / scale)), interpolation=cv2.INTER_NEAREST) # Convert subsampled image to desired color space(s) # OpenCV uses BGR, matplotlib likes RGB img_small_RGB = cv2.cvtColor(img_small, cv2.COLOR_BGR2RGB) img_small_HSV = cv2.cvtColor(img_small, cv2.COLOR_BGR2HSV) img_small_rgb = img_small_RGB / 255. # scaled to [0, 1], only for plotting # Plot and show plot3d(img_small_RGB, img_small_rgb) plt.suptitle(name) plt.show() plot3d(img_small_HSV, img_small_rgb, axis_labels=list("HSV")) plt.suptitle(name) plt.show()
def get_clean_data(): x, y = load_training_data() # Reshape images from 1x4096 to 64x64 x = threshold(x) x = remove_dots(x) x = x.reshape(-1, 64, 64, 1) test_split = 0.1 np.random.seed(113) indices = np.arange(len(x)) np.random.shuffle(indices) _, num_to_index, _ = get_labels() x = x[indices] y = y[indices] y = [num_to_index[yi] for yi in y.tolist()] x_train = np.array(x[:int(len(x) * (1 - test_split))]) y_train = np.array(y[:int(len(x) * (1 - test_split))]) x_test = np.array(x[int(len(x) * (1 - test_split)):]) y_test = np.array(y[int(len(x) * (1 - test_split)):]) return (x_train, y_train), (x_test, y_test)
def train(num_iters): train_data, train_labels = load_training_data() with tf.Graph().as_default(): with tf.Session() as sess: batch_inputs,batch_labels=create_placeholders(100,32,32,3,10) logits=vgg.inference_vgg(batch_inputs) loss=vgg.loss(logits,batch_labels) train_op=vgg.training(loss,0.0001) saver = tf.train.Saver(tf.all_variables()) #load most recent checkpoint ckpt=tf.train.get_checkpoint_state('./checkpoints') if ckpt: saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] global_step=int(global_step) else: print('No checkpoint found') global_step=0 #summary_op=tf.merge_all_summaries() #causes problems with placeholders #summary_writer=tf.train.SummaryWriter('log',sess.graph) #if (global_step==0): if (global_step==0): sess.run(tf.initialize_all_variables()) for iter in range(num_iters): global_step+=1 start_time=time.time() inps,labs=create_batch(iters,train_data,train_labels) feed_dict={ batch_inputs: inps, batch_labels: labs } _, total_loss = sess.run([train_op,loss],feed_dict=feed_dict) end_time=time.time() total_time=(end_time-start_time) if (global_step%5)==0: print('Iteration: %d, Loss: %.2f, Iteration time: %.1f' %(global_step,total_loss,total_time)) #if (iter%50==0): # summary_str=sess.run(summary_op) # summary_writer.add_summary(summary_str,iter) if (global_step%1000==0): checkpoint_path = os.path.join('./checkpoints', 'model.ckpt') saver.save(sess, checkpoint_path, global_step=global_step)
def get_training_data(): print('\ngetting training data') x_train, y_train = load_training_data() Y_train = np_utils.to_categorical(y_train, len(LABELS)) X_val, Y_val = get_validation_data() return x_train, Y_train, X_val, Y_val