def segmentation(img): no_of_frames =1 no_of_batches = int(no_of_frames/batch_size) saver = tf.train.Saver(tf.trainable_variables(), write_version=tf.train.SaverDef.V2) with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) saver.restore(sess, project_dir + "training_logs/model_4/checkpoints/model_4_epoch_50.ckpt") # print("weights loaded") step=0 batch_pointer = 0 for j in range(batch_size): batch_imgs = np.zeros((batch_size, img_height, img_width, 3), dtype=np.float32) img = cv2.resize(img, (img_width, img_height)) img = img - train_mean_channels img=np.expand_dims(img,axis=0) batch_imgs = img batch_feed_dict = model.create_feed_dict(imgs_batch=batch_imgs, early_drop_prob=0.0, late_drop_prob=0.0) logits = sess.run(model.logits, feed_dict=batch_feed_dict) predictions = np.argmax(logits, axis=3) pred_img = predictions[j] pred_img_color = label_img_to_color(pred_img) img = batch_imgs[j] + train_mean_channels #overlayed_img = 0.2*img + 0.8*pred_img_color overlayed_img=1*pred_img_color return[overlayed_img,pred_img_color]
def evaluate_on_val(): random.shuffle(val_data) val_img_paths, val_trainId_label_paths = zip(*val_data) val_batch_losses = [] batch_pointer = 0 for step in range(no_of_val_batches): batch_imgs = np.zeros((batch_size, img_height, img_width, 3), dtype=np.float32) batch_onehot_labels = np.zeros( (batch_size, img_height, img_width, no_of_classes), dtype=np.float32) for i in range(batch_size): # read the next img: img = cv2.imread(val_img_paths[batch_pointer + i], -1) img = img - train_mean_channels batch_imgs[i] = img # read the next label: trainId_label = cv2.imread( val_trainId_label_paths[batch_pointer + i], -1) # convert the label to onehot: onehot_label = np.zeros((img_height, img_width, no_of_classes), dtype=np.float32) onehot_label[layer_idx, component_idx, trainId_label] = 1 batch_onehot_labels[i] = onehot_label batch_pointer += batch_size batch_feed_dict = model.create_feed_dict( imgs_batch=batch_imgs, early_drop_prob=0.0, late_drop_prob=0.0, onehot_labels_batch=batch_onehot_labels) # run a forward pass, get the batch loss and the logits: batch_loss, logits = sess.run([model.loss, model.logits], feed_dict=batch_feed_dict) val_batch_losses.append(batch_loss) print( "epoch: %d/%d, val step: %d/%d, val batch loss: %g" % (epoch + 1, no_of_epochs, step + 1, no_of_val_batches, batch_loss)) if step < 4: # save the predicted label images to disk for debugging and # qualitative evaluation: predictions = np.argmax(logits, axis=3) for i in range(batch_size): pred_img = predictions[i] label_img_color = label_img_to_color(pred_img) cv2.imwrite((model.debug_imgs_dir + "val_" + str(epoch) + "_" + str(step) + "_" + str(i) + ".png"), label_img_color) val_loss = np.mean(val_batch_losses) return val_loss
batch_pointer += batch_size batch_feed_dict = model.create_feed_dict(imgs_batch=batch_imgs, early_drop_prob=0.0, late_drop_prob=0.0) # run a forward pass and get the logits: logits = sess.run(model.logits, feed_dict=batch_feed_dict) print("step: %d/%d" % (step+1, no_of_batches)) # save all predicted label images overlayed on the input frames to results_dir: predictions = np.argmax(logits, axis=3) for i in range(batch_size): pred_img = predictions[i] pred_img_color = label_img_to_color(pred_img) img = batch_imgs[i] + train_mean_channels img_file_name = img_paths[i].split("/")[-1] img_name = img_file_name.split(".png")[0] pred_path = results_dir + img_name + "_pred.png" overlayed_img = 0.3*img + 0.7*pred_img_color #overlayed_img = 1*pred_img_color #imgg=np.concatenate((img,overlayed_img), axis=1) cv2.imwrite(pred_path, overlayed_img) end=t.time() print('time consumed: {:.0f}m {:.0f}s '.format((end-start)//60,(end-start)%60))
def evaluate_on_val(): # random.shuffle(val_data) # val_img_paths, val_trainId_label_paths = zip(*val_data) val_batch_losses = [] batch_pointer = 0 for step in range(no_of_val_batches): batch_imgs = np.zeros((batch_size, img_height, img_width, 1), dtype=np.float32) batch_onehot_labels = np.zeros( (batch_size, img_height, img_width, no_of_classes), dtype=np.float32) batch_existance_labels = np.zeros((batch_size, 4), dtype=np.float32) for i in range(batch_size): # read the next img: img = cv2.imread(val_img_paths[batch_pointer + i], -1) img = cv2.resize(img, (img_width, img_height), interpolation=cv2.INTER_NEAREST) # read the next label: trainId_label = cv2.imread( train_trainId_label_paths[batch_pointer + i], -1) if (np.random.randint(0, 2, 1) == 0): img = img[100:, :, :] trainId_label = trainId_label[100:, :] if (np.random.randint(0, 2, 1) == 0): img, trainId_label = scaler((img, trainId_label)) img, trainId_label = cropper((img, trainId_label)) trainId_label = cv2.resize(trainId_label, (img_width, img_height), interpolation=cv2.INTER_NEAREST) img, trainId_label = normalizer((img, trainId_label)) batch_imgs[i] = img # convert the label to onehot: onehot_label = np.zeros((img_height, img_width, no_of_classes), dtype=np.float32) onehot_label[layer_idx, component_idx, np.int32(trainId_label)] = 1 batch_onehot_labels[i] = onehot_label # train_existance_label = np.array([0, 0, 0, 0]) # train_existance_label[np.unique(trainId_label)[np.unique(trainId_label)>0] - 1] = 1 # read existance label as well train_existance_label = val_existance_labels[batch_pointer + i] batch_existance_labels[i] = train_existance_label batch_pointer += batch_size batch_feed_dict = model.create_feed_dict( imgs_batch=batch_imgs, early_drop_prob=0.0, late_drop_prob=0.0, onehot_labels_batch=batch_onehot_labels, existance_labels_batch=batch_existance_labels) # run a forward pass, get the batch loss and the logits: batch_loss, logits = sess.run([model.loss, model.net.logits], feed_dict=batch_feed_dict) val_batch_losses.append(batch_loss) print( "epoch: %d/%d, val step: %d/%d, val batch loss: %g" % (epoch + 1, no_of_epochs, step + 1, no_of_val_batches, batch_loss)) if step < 4: # save the predicted label images to disk for debugging and # qualitative evaluation: predictions = np.argmax(logits, axis=3) for i in range(batch_size): pred_img = predictions[i] label_img_color = label_img_to_color(pred_img) cv2.imwrite((model.debug_imgs_dir + "val_" + str(epoch) + "_" + str(step) + "_" + str(i) + ".png"), label_img_color) val_loss = np.mean(val_batch_losses) return val_loss