def predict(img_dir, model_dir, img_size=64, colour_channels=3, batch_size=1): checkpoint_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/model') data, category_ref = read_img_sets(img_dir + '/predict', img_size) flat_img_size = flat_img_shape(img_size, colour_channels) num_classes = len(category_ref) x, y_true, keep_prob = variables(flat_img_size, num_classes) logits = model(x, keep_prob, img_size, colour_channels, filter_size=3, neurons=2*img_size, num_classes=num_classes) predict_op = softmax(logits) with tf.Session() as sess: saver = tf.train.Saver() restore_or_initialize(sess, saver, checkpoint_dir) x_predict_batch, y_predict_batch, _, cls_predict_batch = data.train.next_batch(batch_size=1) x_predict_batch = x_predict_batch.reshape(batch_size, flat_img_size) prediction = sess.run([tf.argmax(predict_op, dimension=1)], feed_dict={x: x_predict_batch, keep_prob: 1.0}) return category_ref[prediction[0][0]], cls_predict_batch[0]
def train(img_dir, model_dir, img_size=64, colour_channels=3, batch_size=10, training_epochs=50): log_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/logs/cnn_with_summaries') checkpoint_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/model') data, category_ref = image_loading.read_img_sets(img_dir + '/train', img_size, validation_size=.2) print(data) flat_img_size = flat_img_shape(img_size, colour_channels) num_classes = len(category_ref) x, y_true, keep_prob = variables(flat_img_size, num_classes) logits = model(x, keep_prob, img_size, colour_channels, filter_size=3, neurons=2*img_size, num_classes=num_classes) cost = calulate_cost(logits, y_true) training_op = optimizer(cost) accuracy = calculate_accuracy(logits, y_true) summary_op = tf.summary.merge_all() saver = tf.train.Saver() writer = tf.summary.FileWriter(log_dir, graph=tf.get_default_graph()) with tf.Session() as sess: restore_or_initialize(sess, saver, checkpoint_dir) print("\n\n" + str(training_epochs)) print("\n\n" + str(data.train.num_examples)) print("\n\n" + str(batch_size)) for epoch in range(training_epochs): batch_count = int(data.train.num_examples / batch_size) for i in range(batch_count): x_batch, y_true_batch, _, cls_batch = data.train.next_batch(batch_size) x_batch = x_batch.reshape(batch_size, flat_img_size) x_test_batch, y_test_batch, _, cls_test_batch = data.test.next_batch(batch_size) x_test_batch = x_test_batch.reshape(batch_size, flat_img_size) _, summary = sess.run([training_op, summary_op], feed_dict={x: x_batch, y_true: y_true_batch, keep_prob: 0.5}) writer.add_summary(summary, epoch * batch_count + i) if epoch % 5 == 0: log_progress(sess, saver, cost, accuracy, epoch, test_feed_dict={x: x_test_batch, y_true: y_test_batch, keep_prob: 1.0}, checkpoint_path=os.path.join(checkpoint_dir, 'model.ckpt'))
def predict(img_dir, model_dir, img_size=64, colour_channels=3, batch_size=1): checkpoint_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/model') data, category_ref = image_loading.read_img_sets(img_dir + '/predict', img_size) flat_img_size = flat_img_shape(img_size, colour_channels) num_classes = len(category_ref) x, y_true, keep_prob = variables(flat_img_size, num_classes) logits = model(x, keep_prob, img_size, colour_channels, filter_size=3, neurons=2*img_size, num_classes=num_classes) predict_op = softmax(logits) with tf.Session() as sess: saver = tf.train.Saver() restore_or_initialize(sess, saver, checkpoint_dir) acertou = 0 errou = 0 batch_count = int(data.train.num_examples / batch_size) for i in range(batch_count): x_predict_batch, y_predict_batch, _, cls_predict_batch = data.train.next_batch(batch_size=1) x_predict_batch = x_predict_batch.reshape(batch_size, flat_img_size) prediction = sess.run([tf.argmax(predict_op, dimension=1)], feed_dict={x: x_predict_batch, keep_prob: 1.0}) if category_ref[prediction[0][0]] == cls_predict_batch[0]: print("Acertou: ", category_ref[prediction[0][0]], cls_predict_batch[0]) acertou += 1 else: print("Errou: ", category_ref[prediction[0][0]], cls_predict_batch[0]) errou += 1 print("Porcentagem = %d", (acertou * 100 / (acertou + errou))) return category_ref[prediction[0][0]], cls_predict_batch[0]
def predict(img_dir, model_dir, img_size=64, colour_channels=3, batch_size=803): checkpoint_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/model/') ckpt = tf.train.get_checkpoint_state(checkpoint_dir) checkpoint_dir2 = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/model2') ckpt2 = tf.train.get_checkpoint_state(checkpoint_dir2) log_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/logs/cnn_with_summaries') data, category_ref = read_img_sets(img_dir + '/predict', img_size) flat_img_size = flat_img_shape(img_size, colour_channels) num_classes = len(category_ref) x, y_true = variables(flat_img_size, num_classes) logits = model(x, img_size, colour_channels, filter_size=3, neurons=2 * img_size, num_classes=num_classes) predict_op = softmax(logits) with tf.Session() as sess: saver = tf.train.Saver() writer = tf.summary.FileWriter(log_dir, graph=tf.get_default_graph()) writer.add_graph(tf.get_default_graph()) saver.restore(sess, ckpt.model_checkpoint_path) saver.save(sess, checkpoint_dir2) x_predict_batch, y_predict_batch, idss, cls_predict_batch = data.train.next_batch( batch_size=803) x_predict_batch = x_predict_batch.reshape(batch_size, flat_img_size) start = time.time() prediction = sess.run([tf.argmax(predict_op, dimension=1)], feed_dict={x: x_predict_batch}) end = time.time() timepershot = (end - start) / batch_size idsspath = img_dir + '/predict/' + cls_predict_batch[0] + '/' + idss[0] #CV2 part font = cv2.FONT_HERSHEY_SIMPLEX topleftCornerOfText = (10, 40) topleftCornerOfText2 = (10, 70) topleftCornerOfText3 = (10, 100) bottomleft = (10, 350) fontScale = 1 fontColor = (0, 0, 0) lineType = 2 img2 = cv2.imread(idsspath, cv2.IMREAD_COLOR) cv2.putText(img2, 'Prediction: %s' % (category_ref[prediction[0][0]]), topleftCornerOfText, font, fontScale, fontColor, lineType) cv2.putText(img2, 'Truth: %s' % (cls_predict_batch[0]), topleftCornerOfText2, font, fontScale, fontColor, lineType) cv2.putText(img2, 'Time: %s' % (timepershot), topleftCornerOfText3, font, fontScale, fontColor, lineType) cv2.putText(img2, 'Path: %s' % (idss[0]), bottomleft, font, fontScale, fontColor, lineType) cv2.imshow('imgtest', img2) cv2.waitKey(0) cv2.destroyAllWindows() print(img_dir + '/predict/' + cls_predict_batch[0] + '/' + idss[0]) j = 0 for i in range(batch_size): if (category_ref[prediction[0][i]] == cls_predict_batch[i]): j += 1 print(j, batch_size) return category_ref[prediction[0][0]], cls_predict_batch[0]
def predict(img_dir, model_dir, img_size=224, colour_channels=3, batch_size=1): checkpoint_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/model/') ckpt = tf.train.get_checkpoint_state(checkpoint_dir) checkpoint_dir2 = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/modelpredict') ckpt2 = tf.train.get_checkpoint_state(checkpoint_dir2) log_dir = os.path.join(os.path.abspath(model_dir), 'tensorflow/cnn/logspredict/cnn_with_summaries') data, category_ref = read_img_sets(img_dir + '/predict', img_size) predimgs=data.train.x() print("This number of images in image predict folder: ",predimgs) flat_img_size = flat_img_shape(img_size, colour_channels) num_classes = len(category_ref) x, y_true,keep_prob= variables(flat_img_size, num_classes) logits = model(x, keep_prob, img_size, colour_channels, filter_size=3, neurons=2*img_size, num_classes=num_classes) predict_op = softmax(logits) with tf.Session() as sess: saver = tf.train.Saver(save_relative_paths=True) #restore_or_initialize(sess, saver, checkpoint_dir) writer = tf.summary.FileWriter(log_dir, graph=tf.get_default_graph()) writer.add_graph(tf.get_default_graph()) saver.restore(sess, ckpt.model_checkpoint_path) saver.save(sess, checkpoint_dir2) #print(data._num_examples) x_predict_batch, y_predict_batch, idss, cls_predict_batch = data.train.next_batch(batch_size=predimgs) x_predict_batch = x_predict_batch.reshape(predimgs, flat_img_size) #print(idss[0],idss[1]) print(x_predict_batch) testimg = cv2.imread('test.png') testimg = cv2.resize(testimg, (img_size,img_size), cv2.INTER_LINEAR) #img=img.astype(np.float32) testbin = np.empty((1,(img_size*img_size*3)),np.float32) #testbin=np.vstack((testbin,testimg.flatten())) testbin[0]=testimg.flatten() testbin=np.multiply(testbin, 1.0/255.0) print(testbin) #print(testimg.flatten().shape) start = time.time() prediction = sess.run([tf.argmax(predict_op, dimension=1)], feed_dict={x: x_predict_batch}) #prediction = sess.run([tf.argmax(predict_op, dimension=1)], feed_dict={x: testbin}) #print(prediction[0],cls_predict_batch) #if category_ref[prediction[0][0]] == cls_predict_batch[0]: # print("it is correct") end = time.time() timepershot=(end-start)/predimgs idsspath=img_dir + '/predict/'+cls_predict_batch[0]+'/'+idss[0] font = cv2.FONT_HERSHEY_SIMPLEX topleftCornerOfText = (10,40) topleftCornerOfText2 = (10,70) topleftCornerOfText3 = (10,100) bottomleft = (10,350) fontScale = 1 fontColor = (0,0,0) lineType = 2 img2 = cv2.imread(idsspath, cv2.IMREAD_COLOR) cv2.putText(img2,'Prediction: %s' % (category_ref[prediction[0][0]]), topleftCornerOfText, font, fontScale,fontColor,lineType) cv2.putText(img2,'Truth: %s' % (cls_predict_batch[0]), topleftCornerOfText2, font, fontScale,fontColor,lineType) cv2.putText(img2,'Time: %s' % (timepershot), topleftCornerOfText3, font, fontScale,fontColor,lineType) cv2.putText(img2,'Path: %s' % (idss[0]),bottomleft, font, fontScale,fontColor,lineType) cv2.imshow('imgtest',img2) cv2.waitKey(0) cv2.destroyAllWindows() print(img_dir + '/predict/'+cls_predict_batch[0]+'/'+idss[0]) j=0 for i in range(predimgs): if (category_ref[prediction[0][i]] == cls_predict_batch[i]): j+=1 print(j,predimgs) return category_ref[prediction[0][0]], cls_predict_batch[0]