def main(): img_1 = cv2.imread( 'images/astra_door_align/betty/2019-06-12_16-44-44_ivy_375_78_73_dc93d3_28.jpg' ) img_1 = cv2.cvtColor(img_1, cv2.COLOR_BGR2RGB) img_1 = utils.pre_process_image(img_1, shape=SHAPE) img_2 = cv2.imread( 'images/astra_door_align/jason/2019-06-12_15-42-15_jason_349_146_113_941e14_45.jpg' ) img_2 = cv2.cvtColor(img_2, cv2.COLOR_BGR2RGB) img_2 = utils.pre_process_image(img_2, shape=SHAPE) with tf.Session() as sess: saver = tf.train.import_meta_graph(f'model_out/{CKPT_NAME}.meta', clear_devices=True) saver.restore(sess, f"model_out/{CKPT_NAME}") input_tensor = tf.get_default_graph().get_tensor_by_name( "input_images:0") embedding_tensor = tf.get_default_graph().get_tensor_by_name( "gdc/embedding/Identity:0") f1 = sess.run(embedding_tensor, feed_dict={input_tensor: np.expand_dims(img_1, axis=0)})[0] f1 = sklearn.preprocessing.normalize(np.expand_dims(f1, 0)).flatten() print('--->', f1) f2 = sess.run(embedding_tensor, feed_dict={input_tensor: np.expand_dims(img_2, axis=0)})[0] f2 = sklearn.preprocessing.normalize(np.expand_dims(f2, 0)).flatten() print('--->', f2) dist = np.sum(np.square(f1 - f2)) sim = np.dot(f1, f2.T) print(f'dist: {dist}') print(f'sim: {sim}')
def predict_one_timestep(predict_func, encoder, code_size, initials, x, out_size, iteration): try: img = CvBridge().imgmsg_to_cv2(camera1_msg, "bgr8") img = np.array(img, dtype=np.float) img = img[0:540, 250:840] cv2.imwrite('predictions/current_image.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), 80]) except (CvBridgeError) as e: print(e) else: image = PIL.Image.open('predictions/current_image.jpg') current_scene_image = pre_process_image(image) cv2.imshow( 'Input image', cv2.resize(np.array(current_scene_image.transpose( (1, 2, 0)))[..., ::-1], (0, 0), fx=4, fy=4, interpolation=cv2.INTER_NEAREST)) cv2.waitKey(10) current_scene_image = np.array(current_scene_image, dtype=np.float32) images = np.array([current_scene_image]) _, encoded_images = encode_image(images, encoder) decoded_images = decode_image(encoded_images, encoder) cv2.imshow( 'Reconstructed image', cv2.resize(np.array(decoded_images[0].transpose((1, 2, 0)))[..., ::-1], (0, 0), fx=4, fy=4, interpolation=cv2.INTER_NEAREST)) cv2.waitKey(10) x = np.concatenate([encoded_images[0], x]) newinitials = predict_func([[x]]) raw_prediction = newinitials.pop().astype(theano.config.floatX) if single_dim_out: predicted_values = raw_prediction[:, -1, -1].astype( theano.config.floatX).reshape((len(raw_prediction), )) else: predicted_values = raw_prediction[-1, -1, :].astype(theano.config.floatX) layer = 0 for initial, newinitial in zip(initials, newinitials): if iteration % layer_resolutions[layer // 2] == 0: initial.set_value(newinitial[-1].flatten()) layer += (2 if layer_models[layer // 2] == 'mt_rnn' else 1) layer = min([layer, len(layer_resolutions)]) return predicted_values, newinitials
def next_clip(self, video_name, frames_name, start_frame_index): horizontal_video_path = os.path.join(self.data_dir_horizontal, video_name, '') vertical_video_path = os.path.join(self.data_dir_vertical, video_name, '') frames = [] for i in xrange(start_frame_index, start_frame_index + self.frame_per_clip): frame_path = os.path.join(horizontal_video_path, frames_name[i]) frames.append(load_one_image(frame_path)) for i in xrange(start_frame_index, start_frame_index + self.frame_per_clip): frame_path = os.path.join(vertical_video_path, frames_name[i]) frames.append(load_one_image(frame_path)) clip = mx.ndarray.concatenate(frames, axis=2) clip = pre_process_image(self.data_shape, clip, self.augmentation) clip = post_process_image(clip) return clip
def next_clip(self, video_name, frames_name, start_frame_index): frames = [] for dir in self.data_dir: video_path = os.path.join(dir, video_name, '') for i in xrange(start_frame_index, start_frame_index + self.frame_per_clip): frame_path = os.path.join(video_path, frames_name[i]) frames.append( load_one_image(frame_path, greyscale=self.greyscale, record=self.record, lst_dict=self.lst_dict)) #logging.debug("Start: {}".format(time.asctime(time.localtime(time.time())))) clip = mx.ndarray.concatenate(frames, axis=2) clip = pre_process_image(self.data_shape, clip, self.augmentation) clip = post_process_image(clip) #logging.debug("End: {}".format(time.asctime(time.localtime(time.time())))) #logging.debug('The clip shape is {}'.format(clip.shape)) return clip
def read_images(path, start_index, end_index): """Extract the images into a 4D tensor [image index, channels, y, x]. """ data = np.memmap(str(np.random.randint(1000000)) + '.npy', dtype='uint8', mode='w+', shape=(end_index - start_index, num_channels, image_shape[0], image_shape[1])) sample_index = 0 for index_offset in xrange(0, waypoint_resolution, resolution_step): for j in xrange(start_index + index_offset, end_index, waypoint_resolution): if sample_index % 1000 == 0: sys.stdout.write('\r' + str(sample_index) + ' / ' + str((end_index - start_index))) sys.stdout.flush() # important image = read_image(path + '/' + str(j) + ".jpg") data[sample_index] = pre_process_image(image) sample_index += 1 print '' return data
def GTSRB_Classifier(path, image_GS_test, labels_test): features = tf.placeholder(tf.float32, shape=[None, img_size, img_size, num_channels], name='features') labels_true = tf.placeholder(tf.float32,shape=[None,N_classes], name='y_true') labels_true_cls = tf.argmax(labels_true, dimension=1) fc_layer3, labels_pred, labels_pred_cls = GTSRB_Model(features=features, keep_prob=1.0) correct_prediction = tf.equal(labels_pred_cls, labels_true_cls) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) totalacc = 0.0 with tf.Session() as sess: saver = tf.train.Saver() saver.restore(sess=sess, save_path=path) for i in range(0, 100): if(i == 99): feed_dict_test = {features: image_GS_test[i*126:-1], labels_true: labels_test[i*126:-1], keep_prob:1.0} else: feed_dict_test = {features: image_GS_test[i*126:(i+1)*126], labels_true: labels_test[i*126:(i+1)*126], keep_prob:1.0} acc = sess.run(accuracy,feed_dict=feed_dict_test) print(acc) totalacc = totalacc + acc print("Accuracy on test set: {0:>6.1%}".format(totalacc/100)) if __name__ == "__main__": test_data_dir = '/media/dsgDisk/dsgPrivate/liuaishan/GTSRB/data/test.p' weights_dir = '/media/dsgDisk/dsgPrivate/liuaishan/GTSRB/model/test/GTSRB_best_test' with open(test_data_dir, 'rb') as f: dataset = pickle.load(f) image_GS_test = np.asarray([pre_process_image(item) for item in dataset['data']]).astype(np.float32) labels_test = OHE_labels(dataset['labels'], N_classes).astype(np.float32) GTSRB_Classifier(weights_dir, image_GS_test, labels_test)
saver.restore(sess=sess, save_path=path) for i in range(0, 100): if (i == 99): feed_dict_test = { features: image_GS_test[i * 126:-1], labels_true: labels_test[i * 126:-1], keep_prob: 1.0 } else: feed_dict_test = { features: image_GS_test[i * 126:(i + 1) * 126], labels_true: labels_test[i * 126:(i + 1) * 126], keep_prob: 1.0 } acc = sess.run(accuracy, feed_dict=feed_dict_test) print(acc) totalacc = totalacc + acc print("Accuracy on test set: {0:>6.1%}".format(totalacc / 100)) if __name__ == "__main__": test_data_dir = '/media/dsgDisk/dsgPrivate/liuaishan/GTSRB/data/test.p' weights_dir = '/media/dsgDisk/dsgPrivate/liuaishan/GTSRB/model/test/GTSRB_best_test' with open(test_data_dir, 'rb') as f: dataset = pickle.load(f) image_GS_test = np.asarray([ pre_process_image(item) for item in dataset['data'] ]).astype(np.float32) labels_test = OHE_labels(dataset['labels'], N_classes).astype(np.float32) GTSRB_Classifier(weights_dir, image_GS_test, labels_test)