def Generator(lines, start, end): images = [] steer_angles = [] for index, row in lines.loc[start:end].iterrows(): source_path = lines['center'][index] filename = source_path.split('/')[-1] current_path = './data/IMG/' + filename image = cv2.imread(current_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = preprocess_image(image) steer_angle = float(lines['steering'][index]) images.append(image) steer_angles.append(steer_angle) source_path = lines['left'][index] filename = source_path.split('/')[-1] current_path = './data/IMG/' + filename image = cv2.imread(current_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = preprocess_image(image) steer_angle = float(lines['steering'][index]) + 0.25 images.append(image) steer_angles.append(steer_angle) flipable = np.random.random() source_path = lines['right'][index] filename = source_path.split('/')[-1] current_path = './data/IMG/' + filename image = cv2.imread(current_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = preprocess_image(image) steer_angle = float(lines['steering'][index]) - 0.25 images.append(image) steer_angles.append(steer_angle) return images, steer_angles
def add_style_loss(vgg, style_image_path, vgg_layers, vgg_output_dict, img_width, img_height, weight): style_img = preprocess_image(style_image_path, img_width, img_height) print('Getting style features from VGG network.') style_layers = [ 'block1_conv2', 'block2_conv2', 'block3_conv3', 'block4_conv3' ] style_layer_outputs = [] for layer in style_layers: style_layer_outputs.append(vgg_output_dict[layer]) vgg_style_func = K.function([vgg.layers[-15].input], style_layer_outputs) style_features = vgg_style_func([style_img]) # Style Reconstruction Loss for i, layer_name in enumerate(style_layers): layer = vgg_layers[layer_name] feature_var = K.variable(value=style_features[i][0]) style_loss = StyleReconstructionRegularizer( style_feature_target=feature_var, weight=weight)(layer) layer.add_loss(style_loss)
def eval_src(model, data_loader): loss = 0 accuracy = 0 model.eval() with torch.no_grad(): criterion = nn.CrossEntropyLoss() correct = 0 total = 0 for (images, labels) in data_loader.image_gen( split_type='val', batch_size=params.eval_batch_size): images = preprocess_image(array=images, split_type='val', use_gpu=params.gpu_flag, gpu_name=params.gpu_name) labels = torch.tensor(labels, dtype=torch.long) if (params.gpu_flag == True): labels = labels.cuda(params.gpu_name) with torch.no_grad(): _, preds = model(images) loss += criterion(preds, labels).item() _, predicted = torch.max(preds.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() loss /= data_loader.size['val'] # accuracy /= len( data_loader ) accuracy = correct / total print("Avg Loss = {}, Avg Accuracy = {:2%}".format(loss, accuracy))
def model_output(model, data_loader, split_type, repeat_num ): feature_array = [] label_array = [] for step, (images, labels) in zip(range(int(repeat_num*data_loader.size[split_type]/params.eval_batch_size)), cycle(data_loader.image_gen(split_type = split_type, batch_size=params.eval_batch_size))): start = time.time() images = preprocess_image( array = images, split_type = split_type, use_gpu = params.gpu_flag, gpu_name= params.gpu_name ) end = time.time() print('time taken ', end - start ) feature, _ = model(images) feature = feature.cpu().detach().numpy() for f,l in zip(feature, labels): feature_array.append(f) label_array.append(l) if((step +1) % 50 == 0): print( 'step = ', step) ret_dict = {'feature': np.array(feature_array), 'label' : np.array(label_array) } return ret_dict
def im_to_json(im): """Here's where the magic happens""" items = ['sierra nevada stout', 'lagunitas ipa', 'founders centennial'] prices = [5.50, 6.50, 7.50] subtotal = sum(prices) tax = subtotal * 0.07 total = subtotal + tax bill_dict = { 'items': [{ 'name': item, 'price': price, 'quantity': 1 } for item, price in zip(items, prices)], 'subtotal': subtotal, 'tax': tax, 'total': total } preprocessed_im = preprocess_image(im) buf = io.BytesIO() plt.imsave(buf, preprocessed_im) im_data = buf.getvalue() bill_dict = read_receipt(im_data) return jsonify(bill_dict)
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) # Add the preprocessing step image_array = preprocess_image(image_array) steering_angle = float(model.predict(image_array[None, :, :, :], batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def replaceWithPhoto(np_row): image_location = np_row[0].decode('UTF-8').strip() image = imread(image_location).astype(np.float32) image = preprocess_image(image) image = normalize(image) imgplot = plt.imshow(image) plt.show() return np.array([image, np_row[1]])
def train_network(data_loader, dump_location): model = ResNetFc(resnet_name='ResNet50', use_bottleneck=False, bottleneck_dim=256, new_cls=True, class_num=params.num_class) if (params.gpu_flag): model.cuda(params.gpu_name) optimizer = torch.optim.Adam( model.parameters(), lr=params.pretrain_lr, ) criterion = nn.CrossEntropyLoss() for epoch in range(params.num_epochs_pretrain): model.train() for step, (images, labels) in enumerate(data_loader.image_gen('train')): images = preprocess_image(array=images, split_type='train', use_gpu=params.gpu_flag, gpu_name=params.gpu_name) labels = torch.tensor(labels, dtype=torch.long) if (params.gpu_flag == True): labels = labels.cuda(params.gpu_name) optimizer.zero_grad() _, preds = model(images) loss = criterion(preds, labels) loss.backward() optimizer.step() # print step info if ((step + 1) % params.log_step_pre == 0): print("Epoch [{}/{}] Step [{}/{}]: loss={}".format( epoch + 1, params.num_epochs_pretrain, step + 1, int(data_loader.size['train'] / data_loader.batch_size), loss.data.item())) # print(list(source_classifier.parameters())) # eval model on test set eval_src(model, data_loader) # save model parameters torch.save(model, dump_location) print('model saved at location ' + dump_location) return model
def run_thining(name, img): preprocessed = preprocess_image(img, sharpen=True, gaussian_blur=True) # thinned = kmm(preprocessed) thinned = k3m(preprocessed) rgb_image = prepare_to_print(thinned) cv.imshow('in', img) cv.imshow('out', rgb_image) cv.waitKey(0)
def post(self): f = request.files['image'] image_name = 'images/{0}_{1}'.format(os.getpid(), secure_filename(f.filename)) f.save(image_name) data = preprocess_image(image_name) # TODO: # 1. Make a SQL Query to Database to get the Item or related Items # 2. Return the Result to the Mobile return jsonify(data)
def post(self): f = request.files['image'] image_name = 'images/{0}_{1}'.format(os.getpid(), secure_filename(f.filename)) f.save(image_name) data = preprocess_image(image_name) # TODO: #readproductdata from the database #productData=DB.GetproductData(data.code,data.) # 2. Return productData to the Mobile return jsonify(data)
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = preprocess.preprocess_image(image_array) # # Gavin's additions: crop and rescale so we can use VGG # corners = (80, 0), (240, 160) # for idx, image in enumerate(image_array) # cropped_image = image[corners[0][1]:corners[1][1], corners[0][0]:corners[1][0], :] # cropped_and_upscaled_image = cv2.resize(cropped_image, (224, 224)) # image_array[idx] = cropped_and_upscaled_image steering_angle = float( model.predict(image_array[None, :, :, :], batch_size=1)) # min_speed = 8 # max_speed = 10 # if float(speed) < min_speed: # throttle = 1.0 # elif float(speed) > max_speed: # throttle = -1.0 # else: # throttle = 0.1 try: if abs(steering_angle) > 0.1 and float(speed) > 10: throttle = 0.01 else: throttle = 0.15 except: print("Error with speed value: ", speed) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def read_and_decode(tfrecord_file, batch_size, is_training=False): filename_queue = tf.train.string_input_producer([tfrecord_file]) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) img_features = tf.parse_single_example(serialized_example, features={ 'image/label': tf.FixedLenFeature([], tf.int64), 'image/height': tf.FixedLenFeature([], tf.int64), 'image/width': tf.FixedLenFeature([], tf.int64), 'image/channel': tf.FixedLenFeature([], tf.int64), 'image/encoded': tf.FixedLenFeature([], tf.string), }) h = tf.cast(img_features['image/height'], tf.int32) w = tf.cast(img_features['image/width'], tf.int32) c = tf.cast(img_features['image/channel'], tf.int32) image = tf.decode_raw(img_features['image/encoded'], tf.uint8) image = tf.reshape(image, [h, w, c]) label = tf.cast(img_features['image/label'], tf.int64) label = tf.reshape(label, [1]) image = tf.image.resize_images(image, (INPUT_HEIGHT, INPUT_WIDTH)) image = tf.reshape(image, [INPUT_HEIGHT, INPUT_WIDTH, INPUT_CHANNEL]) # data augmentation image = preprocess.preprocess_image(image, INPUT_HEIGHT, INPUT_WIDTH, is_training=is_training) image_batch, label_batch = tf.train.shuffle_batch([image, label], batch_size=batch_size, num_threads=4, min_after_dequeue=100, capacity=200) return image_batch, tf.reshape(label_batch, [batch_size])
def read_image(file_path): img_org = cv2.imread(file_path) img = cv2.imread(file_path, 0) # cv2.imshow('grayscale', img) preprocessed = preprocess.preprocess_image(img) # cv2.imshow('preprocessed', preprocessed) # return list of 4 corner points corners = preprocess.find_corners(preprocessed) cropped = preprocess.crop_and_warp(img, corners) # cv2.imshow('cropped', cropped) cv2.waitKey(0)
def _preprocess_image(byte, label): image = preprocess.preprocess_image(image_size=image_size, image_bytes=byte, is_training=is_training, use_color_jitter=use_color_jitter, use_randaug=use_randaug) image = normalize(image, method=normalize_method) if include_background: num_label = 1001 else: num_label = 1000 label -= 1 if use_one_hot: label = one_hot(label, num_label) return image, label
def evaluate_on_batch(self, data): input_files = data["files"] input_targets = data["targets"] outputs = { "metrics": [ { "name": "Accuracy", "values": [], "reduction": "mean", } ], "errors": [], } for i, file in enumerate(input_files): if file["content_type"] != "application/dicom": continue ds = pydicom.dcmread(BytesIO(file["content"])) image = ds.pixel_array x = preprocess_image(image) y_prob = self.model.predict(x) y_classes = y_prob.argmax(axis=-1) class_index = y_classes[0] probability = y_prob[0][class_index] target = next( item for item in input_targets if item["resource_scope"] == "INSTANCE" and item["resource_uid"] == ds.SOPInstanceUID ) if target["target_type"] != "NONE": outputs["metrics"][0]["values"].append( 1 if int(class_index) == int(target["target_class_index"]) else 0 ) else: outputs["errors"].append( { "resource_uid": str(ds.SOPInstanceUID), "error_message": "Missing annotation for resource", } ) return outputs
def predict(self, data): """ See https://github.com/mdai/model-deploy/blob/master/mdai/server.py for details on the schema of `data` and the required schema of the outputs returned by this function. """ input_files = data["files"] input_annotations = data["annotations"] input_args = data["args"] outputs = [] for file in input_files: if file["content_type"] != "application/dicom": continue ds = pydicom.dcmread(BytesIO(file["content"])) image = ds.pixel_array original_dims = image.shape x, offset = preprocess_image(image) imgsize = x.shape[1] mask = self.model.predict(x)[0, :, :, 0] > 0.5 mask = mask.astype(np.uint8) # Handle padding and resize row_start, row_end = offset[0], imgsize - offset[0] col_start, col_end = offset[1], imgsize - offset[1] mask = mask[row_start:row_end, col_start:col_end] mask = cv2.resize(mask, (original_dims[1], original_dims[0])).astype(np.uint8) # Each contour should create a new annotation model output for contour in find_contours(mask, 0): data = {"vertices": [[(v[1]), (v[0])] for v in contour.tolist()]} output = { "type": "ANNOTATION", "study_uid": str(ds.StudyInstanceUID), "series_uid": str(ds.SeriesInstanceUID), "instance_uid": str(ds.SOPInstanceUID), "class_index": 0, "data": data, } outputs.append(output) return outputs
def path_to_tensor(self, image_path): # convert an image to tensor preped_img = None if self.face_crop == True: preped_img = preprocess_image(image_path, None, crop_dim=224) if preped_img is None: return [] else: # use the whole picture and first resize the image reserving its width/heigh ratio and ## if one side of short of 224 padding preped_img = resize_image(image_path, None, size=224, random_padding_border_color=True) logger.info("Processed image {} shape {}".format( image_path, preped_img.shape)) return np.expand_dims(np.array(preped_img, dtype='float'), axis=0)
def tool(args=None): parser = argparse.ArgumentParser( description='Simple script for usage of tool') parser.add_argument('--directory', help='Path to the Directory containing the image') # parser.add_argument('--all', help='') parser.add_argument('--name', help='Image to be used') parser_args = parser.parse_args(args) # print(parser) name = parser_args.name directory = parser_args.directory if parser_args.directory == None: raise ValueError('Must provide the directory name') if parser_args.name == None: raise ValueError('Must provide the image name in the directory') print("Loading Models ...") rpn_model = torch.load('rpn_99.pth', map_location=torch.device('cpu')) rpn_model.eval() classifier_model = Net() classifier_model.load_state_dict( torch.load('classifier_final.pth', map_location=torch.device('cpu'))) classifier_model.eval() print("Models Loaded") print() transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) ]) print("Obtaining image and checking for cosmic ray ...") arr = preprocess_image(directory, name) if arr is not None: img = Image.fromarray(arr) img_PIL = img.resize((1024, 1024)).convert('RGB') img_tensor = transform(img_PIL) Show_Final_Predictions(img_tensor.view(1, 3, 1024, 1024), img_PIL, name, rpn_model, classifier_model) print() print("Saved to directory- \'predictions/\'")
def eval_src(source_encoder, source_classifier, data_loader, gpu_flag=False, gpu_name='cuda:0'): loss = 0 accuracy = 0 source_encoder.eval() source_classifier.eval() criterion = nn.CrossEntropyLoss() correct = 0 total = 0 for (images, labels) in data_loader.image_gen(split_type='val'): images = preprocess_image(array=images, split_type='val', use_gpu=gpu_flag, gpu_name=gpu_name) labels = torch.tensor(labels, dtype=torch.long) if (gpu_flag == True): labels = labels.cuda(gpu_name) preds = source_classifier(source_encoder(images)) loss += criterion(preds, labels).item() _, predicted = torch.max(preds.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() # pred_cls = preds.data.max(1)[1] # print(pred_cls.eq(labels.data).cpu().sum()) # accuracy += pred_cls.eq(labels.data).cpu().sum() / len(labels) loss /= data_loader.size['val'] # accuracy /= len( data_loader ) accuracy = correct / total print("Avg Loss = {}, Avg Accuracy = {:2%}".format(loss, accuracy))
def loading_worker(): while True: try: cur_img_ind = loading_queue.get(block=False) except Empty: if not processing_queue.empty(): time.sleep(0.1) continue else: break cur_image_path = os.path.join( coco_images_dir, images_data[cur_img_ind]['file_name']) preprocessed_img = preprocess_image(cur_image_path, model_resolution) loaded_img_queue.put( (images_data[cur_img_ind]['id'], preprocessed_img)) # print('thread: {}; loaded: {}'.format(threading.current_thread().name, cur_image_path)) loading_queue.task_done()
def who_is_it(img, percent): img_representation = vgg_face_descriptor.predict( preprocess.preprocess_image_n(cv2.resize(img, (224, 224))))[0, :] l = img_representation.shape[0] ans = 'not in database' res = [999, 999] for image_path in os.listdir(path): input_path = os.path.join(path, image_path) curr = vgg_face_descriptor.predict( preprocess.preprocess_image(input_path))[0, :] cosine_similarity = mf.findCosineDistance(img_representation, curr) euclidean_distance = mf.findEuclideanDistance(img_representation, curr) if cosine_similarity < ( percent / 10) and euclidean_distance < (percent / 100) * l: if cosine_similarity < res[0] and euclidean_distance < res[1]: ans = image_path.split("_")[0] return ans
def predict(self, image, feature_names): try: image, batchsize = preprocess_image(image) data_blob_shape = self.net.blobs['data'].data.shape data_blob_shape = list(data_blob_shape) self.net.blobs['data'].reshape(batchsize, data_blob_shape[1], data_blob_shape[2], data_blob_shape[3]) if batchsize > 1: self.net.blobs['data'].data[...] = map( lambda x: self.transformer.preprocess('data', x), image) else: self.net.blobs['data'].data[...] = self.transformer.preprocess( 'data', image) out = self.net.forward() ret = postprocess_result(out) return np.array(ret) except: print(traceback.format_exc())
def Generator1(lines, start, end): images = [] steer_angles = [] for index, row in lines.loc[start:end].iterrows(): col = np.random.choice(['center', 'left', 'right']) source_path = lines[col][index] filename = source_path.split('\\')[-1] current_path = './alex/IMG/' + filename if col == 'left': steer_angle = float(lines['steering'][index]) + 0.25 elif col == 'center': steer_angle = float(lines['steering'][index]) else: steer_angle = float(lines['steering'][index]) - 0.25 image1 = cv2.imread(current_path) image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB) image1 = preprocess_image(image1) images.append(image1) steer_angles.append(steer_angle) return images, steer_angles
def telemetry(sid, data): # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = preprocess_image(image_array) image_array = normalize(image_array) transformed_image_array = image_array[None, :, :, :] # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float(model.predict(transformed_image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. throttle = 0.3 print(steering_angle, throttle) send_control(steering_angle, throttle)
def generator(df, inputs, targets): count = 0 while (True): #Shuffle df = df.sample(frac=1).reset_index(drop=True) if epoch_size is not None: # Limit the epoch size df = df.iloc[:epoch_size] # Bad hack for idx in range(len(df)): # Define a random threshold for each image taken threshold = np.random.uniform() row = df.iloc[idx] if (abs(row['steering'])) < threshold: #Skip continue image_path = row['path'] img = cv2.imread(image_path) if row['mirror']: img = img[:, ::-1, :] if row['shadow']: img = add_random_shadow(img) img = change_brightness(img) img = preprocess.preprocess_image(img) img = img[np.newaxis, :, :, :] inputs[count] = img targets[count] = row['steering'] count += 1 if count == batch_size: yield inputs, targets inputs = np.zeros([batch_size, *image_size]) targets = np.zeros([batch_size]) count = 0
def makePredictions(model, img_path, classes: list): image = preprocess_image(img_path) predic = model.predict(image) index = np.argmax(predic) return classes[index]
def _preprocess_image(image_bytes): """Preprocess a single raw image.""" image = preprocess.preprocess_image( image_bytes=image_bytes, is_training=False, image_size=image_size) return image
def main(_): if not FLAGS.dataset_name: raise ValueError( 'You must supply the dataset file path with --dataset_name') tf.logging.set_verbosity(tf.logging.DEBUG) with tf.Graph().as_default(): # Config model_deploy. Keep TF Slim Models structure. # Useful if want to need multiple GPUs and/or servers in the future. deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=0, num_replicas=1, num_ps_tasks=0) dataset = read_records_classification.input_reader( FLAGS.dataset_name, num_samples) # =================================================================== # # Create a dataset provider and batches. # =================================================================== # with tf.device(deploy_config.inputs_device()): with tf.name_scope('data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, shuffle=True) #[image, bbox] = provider.get(['image', 'object/bbox']) image, label = provider.get(['image', 'label']) image, height, width = preprocess.preprocess_image(image) label_fil = open('out/label_fil', 'w') with tf.Session() as sess: # Initialize all global and local variables init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) # Create a coordinator and run all QueueRunner objects coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) # images, keypoints = sess.run([images, keypoints]) count = 1 for batch_index in range(15): img, label_ = sess.run([image, label]) img = img.astype(np.float32) im_height, im_width, channels = img.shape img = np.reshape(img, [im_height, im_width, 3]) print(im_height, im_width) fig = plt.figure(figsize=(20, 20)) plt.imshow(img) fig.savefig('out/test_{}.png'.format(count)) count = count + 1 label_fil.write(str(label_) + '\n') # print(bbx) ''' bbxs = bbx.tolist() for bbx in bbxs: ymin, xmin, ymax, xmax = bbx if math.isnan(xmin): print(bbx) img = img.astype(np.float32) im_height, im_width, channels = img.shape img = np.reshape(img, [im_height, im_width, 3]) print(i_heigth, i_width) fig = plt.figure(figsize=(1, 1)) plt.imshow(img) fig.savefig('out_bad_images/test_{}.png'.format(count)) count = count + 1 ''' # Stop the threads coord.request_stop() # Wait for threads to stop coord.join(threads) #print(label_fil) label_fil.close()
import cv2 import numpy as np import preprocess import solver from nn import NeuralNetwork original = preprocess.original_image cv2.imshow('original', original) processed = preprocess.preprocess_image(original) cv2.imshow('processed', processed) corners = preprocess.find_corners(processed) cropped, projection_mat = preprocess.crop_and_warp(original, corners) squares = preprocess.infer_grid(cropped) digits = preprocess.get_digits(cropped, squares, 28) digits_ = [] for i in range(9): for j in range(9): # row_ = [] digit = digits[9 * j + i].copy() digits_.append(digit) out, tiles = preprocess.show_digits( digits_, show=True, save=True) # cv2.imshow('digit', digits[1]) ''' Solver Method being under development with unhandled recursion, please do not run '''