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.crop(image_array, 0.35, 0.1) image_array = preprocess.resize(image_array, new_dimension=(64, 64)) transformed_image_array = image_array[None, :, :, :] steering_angle = float( model.predict(transformed_image_array, 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 root(): filepath, label, serial = next(im_gen) im = Image.open(filepath) assert im.format == "GIF", im.format assert im.n_frames == 16, im.n_frames N = 52 w, h = im.size images = [] M0_old = None M_mask = np.zeros((h, w), np.uint8) for ix in (3, 7, 11, 15): first = (M0_old is None) im.seek(ix) im_row = [] M0 = np.array(im.convert("RGB")) im_row.append(M0) M0 = cv2.cvtColor(M0, cv2.COLOR_RGB2GRAY) if first: im_row.append(M0) else: Mdiff = cv2.subtract(M0_old, M0) im_row.append(0xff - Mdiff) if first: Mt = extract_c0(M0) else: Mt = extract_c123(M0, M0_old) M0_old = M0 Mt_inv = 0xff - Mt Mt = 0xff - np.bitwise_and(Mt_inv, 0xff - M_mask) M_mask = np.bitwise_or(M_mask, Mt_inv) im_row.append(Mt) Mt = crop(Mt, first) im_row.append(Mt) images.append(im_row) im.close() ctx = { "images": images, "label": label, "serial": serial, } return render_template("label_server.html", **ctx)
def generate_dataset(dataset_dir, crop_size, img_list, label_list): img_path = os.path.join(dataset_dir, 'img') label_path = os.path.join(dataset_dir, 'label') visualize_gt_path = os.path.join(dataset_dir, 'visualize_gt') if (not os.path.exists(img_path)): os.mkdir(img_path) if (not os.path.exists(label_path)): os.mkdir(label_path) if (not os.path.exists(visualize_gt_path)): os.mkdir(visualize_gt_path) for i in range(len(img_list)): crop(img_list[i], label_list[i], crop_size, crop_size, prefix='%d' % (i + 1), save_dir=dataset_dir, crop_label=True) generate_trainval_list(dataset_dir) write_train_list(dataset_dir)
def predict_captcha(im_data, model): fp = BytesIO(im_data) im = Image.open(fp) assert im.format == "GIF", im.format assert im.n_frames == 16, im.n_frames N = CAPTCHA_SIZE w, h = im.size Xlist = [] ylist = [] M0_list = [] M0_last = None M_mask = np.zeros((h, w), dtype=np.uint8) M_merge = np.full((h, w), 0xff, dtype=np.uint8) for ix in (3, 7, 11, 15): im.seek(ix) M0 = np.array(im.convert('RGB')) M0 = cv2.cvtColor(M0, cv2.COLOR_RGB2GRAY) M0_list.append(M0) M_merge = np.min([M_merge, M0], axis=0) for cix, M0 in enumerate(M0_list): first = (M0_last is None) if first: Mt = extract_c0_v2(M0, M_merge) else: Mt = extract_c123(M0, M0_last) M0_last = M0 Mt_inv = 0xff - Mt Mt = 0xff - np.bitwise_and(Mt_inv, 0xff - M_mask) M_mask = np.bitwise_or(M_mask, Mt_inv) Mt = crop(Mt, first) Xlist.append(Mt) im.close() fp.close() Xlist = np.array(Xlist, dtype=np.float32).reshape(-1, 1, N, N) ylist = model(torch.from_numpy(Xlist)) code = ''.join(CAPTCHA_LABELS[ix] for ix in torch.argmax(ylist, dim=1)) return code
def test(): """ Train the model **input: ** *dataset: (String) Dataset folder to used *ckpt: (String) [Optional] Path to the ckpt file to restore """ ckpt = './outputs/checkpoints/SAR' validation_data_dir = './data/Test' img_width, img_height = 100, 100 # Load name of id classname = [ '2S1', 'BMP-2', 'BRDM-2', 'BTR-70', 'BTR-60', 'D7', 'T62', 'T72', 'ZIL131', 'ZSU234' ] # Get Test dataset model = ModelSAR("SAR", output_folder=None) # Load the model model.load(ckpt) for i in range(1): test_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=0) validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_height, img_width), batch_size=2425, class_mode='sparse') filenames = validation_generator.class_indices print(filenames) X_test, y_test = validation_generator.next() X_test = crop(X_test) #X_test = blur(X_test, pow(10, 1 + i * 0.2)) #X_test = oclusion(X_test, 0.06*(1 + i )) #X_test = noise(X_test,0.01*(i+1)) # Evaluate all the dataset loss, acc, predicted_class = model.evaluate_dataset( np.expand_dims(X_test[:, :, :, 1], axis=3), y_test) print("Accuracy = ", acc) print("Loss = ", loss) # Get the confusion matrix cnf_matrix = confusion_matrix(y_test, predicted_class) # Plot the confusion matrix plt.figure(figsize=(5.2, 5)) plot_confusion_matrix(cnf_matrix, classes=classname, title='Confusion matrix, without normalization') plt.show()
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.crop(image_array, 0.35, 0.1) image_array = preprocess.resize(image_array, new_dim=(64, 64)) 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 throttle = 0.2 if float(speed) < 10 and float(speed) > 5: throttle = 0.5 print('{:.5f}, {:.1f}'.format(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))
def __init__(self, T1_path, IR_path, T2_path, is_transform, is_crop, is_hist, forest): self.T1_path = T1_path self.IR_path = IR_path self.T2_path = T2_path self.is_transform = is_transform self.is_crop = is_crop self.is_hist = is_hist self.forest = forest self.n_classes = 11 self.T1mean = 0.0 self.IRmean = 0.0 self.T2mean = 0.0 #read data T1_nii = to_uint8(readVol(self.T1_path)) IR_nii = IR_to_uint8(readVol(self.IR_path)) T2_nii = to_uint8(readVol(self.T2_path)) #histeq if self.is_hist: T1_nii = histeq(T1_nii) #stack T1_stack_list = get_stacked(T1_nii, self.forest) IR_stack_list = get_stacked(IR_nii, self.forest) T2_stack_list = get_stacked(T2_nii, self.forest) #crop if self.is_crop: region_list = calc_max_region_list( calc_crop_region(T1_stack_list, 50, 5), self.forest) self.region_list = region_list T1_stack_list = crop(T1_stack_list, region_list) IR_stack_list = crop(IR_stack_list, region_list) T2_stack_list = crop(T2_stack_list, region_list) #get mean ''' T1mean,IRmean,T2mean=0.0,0.0,0.0 for samples in T1_stack_list: for stacks in samples: T1mean=T1mean+np.mean(stacks) self.T1mean=T1mean/(len(T1_stack_list)*len(T1_stack_list[0])) for samples in IR_stack_list: for stacks in samples: IRmean=IRmean+np.mean(stacks) self.IRmean=IRmean/(len(IR_stack_list)*len(IR_stack_list[0])) for samples in T2_stack_list: for stacks in samples: T2mean=T2mean+np.mean(stacks) self.T2mean=T2mean/(len(T2_stack_list)*len(T2_stack_list[0])) ''' self.T1mean = 94.661544495 self.IRmean = 88.574705283 self.T2mean = 32.376038631 #transform if self.is_transform: for stack_index in range(len(T1_stack_list)): T1_stack_list[stack_index], \ IR_stack_list[stack_index], \ T2_stack_list[stack_index]= \ self.transform( \ T1_stack_list[stack_index], \ IR_stack_list[stack_index], \ T2_stack_list[stack_index]) # data ready self.T1_stack_list = T1_stack_list self.IR_stack_list = IR_stack_list self.T2_stack_list = T2_stack_list
def train(): """ Train the model **input: ** *dataset: (String) Dataset folder to used *ckpt: (String) [Optional] Path to the ckpt file to restore *output: (String) [Optional] Path to the output folder to used. ./outputs/ by default """ ckpt = './outputs/checkpoints/SAR' output_folder = None train_data_dir = './data/Train' validation_data_dir = './data/Train' img_width, img_height = 100, 100 NB_LABELS = 10 nb_train_samples = 2747 batch_size = 16 lr = 0.0002 train_datagen = ImageDataGenerator(rescale=1. / 255) valid_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='sparse', shuffle=True) valid_generator = valid_datagen.flow_from_directory( validation_data_dir, target_size=(img_height, img_width), batch_size=nb_train_samples, class_mode='sparse', shuffle=True) # Init model model = ModelSAR("SAR", output_folder, NB_LABELS) if ckpt is not None: model.init() else: model.load(ckpt) # Training pipeline for batch_id in range(40000): x_batch, y_batch = train_generator.next() x_batch = random_crop(x_batch) ### Training cost, acc = model.optimize(np.expand_dims(x_batch[:, :, :, 1], axis=3), y_batch, lr) print("Batch ID = %s, loss = %s, acc = %s" % (batch_id, cost, acc)) ### Validation if batch_id % 20 == 0: # Plot the last results X_valid, y_valid = valid_generator.next() X_valid = crop(X_valid) print("Evaluate full validation dataset ...") loss, acc, _ = model.evaluate_dataset( np.expand_dims(X_valid[:, :, :, 1], axis=3), y_valid) print("Current loss: %s Current acc: %s" % (loss, acc)) model.save()