def next_batch(X, Y): n_samples = len(X) if n_samples != len(Y): raise ValueError("Unmatched number of samples and label data") if n_samples < config.BATCH_SIZE: raise ValueError("Number of samples is less than batch size") if n_samples != len(next_batch.orders): next_batch.orders = list(range(n_samples)) if next_batch.offset + config.BATCH_SIZE > n_samples: next_batch.offset = 0 random.shuffle(next_batch.orders) X_batch = [] Y_batch = [] for _ in range(config.BATCH_SIZE): im = cv2.imread( os.path.join(config.TRAINING_DATA_DIR, X[next_batch.offset]), cv2.IMREAD_COLOR) if next_batch.offset % 2 == 0: X_batch.append( model.preprocess(cv2.flip(im, 1), config.INPUT_IMAGE_CROP)) Y_batch.append(-Y[next_batch.offset]) else: X_batch.append(model.preprocess(im, config.INPUT_IMAGE_CROP)) Y_batch.append(Y[next_batch.offset]) next_batch.offset += 1 return X_batch, Y_batch
def train_and_predict(): print('Loading and preprocessing data...') data, labels = load_data('data/train/final', 'data/train/labels.csv') print('preprocessing...') imgs_train = preprocess(data) imgs_train = np.array(imgs_train) labels_train = np.array(labels) categorical_labels_train = to_categorical(labels_train) data, labels = load_data('data/test/final', 'data/test/labels.csv') imgs_test = preprocess(data) imgs_test = np.array(imgs_test) labels_test = np.array(labels) categorical_labels_test = to_categorical(labels_test) print('Done.') print('-') print('Creating model...') model = get_model() print('Done') print('-') print('Training model...') print('-') model.fit(imgs_train, categorical_labels_train, batch_size=32, epochs=15, verbose=True, validation_split=0.1, shuffle=True) print('Done training.') print('Score: {}'.format(model.evaluate(imgs_test, categorical_labels_test))) print('Saving model as model.h5') model.save('model.h5') print('Done saving.')
def _get_outputs_from_inputs(input_tensors, model, output_collection_name): inputs = tf.to_float(input_tensors) preprocessed_inputs = model.preprocess(inputs) output_tensors = model.predict(preprocessed_inputs) postprocessed_tensors = model.postprocess(output_tensors) return _add_output_tensor_nodes(postprocessed_tensors, output_collection_name)
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 = preprocess(np.asarray(image)) 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 convert_dataset(model, dataset): model_in = dataset[:, 1].reshape(-1) model_out = dataset[:, 0].flatten().astype(np.int) model_out = torch.autograd.Variable(torch.from_numpy(model_out)).cuda() model_in = model.preprocess(model_in) model_in = torch.autograd.Variable(model_in.cuda()) return (model_in, model_out)
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) # frames incoming from the simulator are in RGB format image_array = cv2.cvtColor(np.asarray(image), code=cv2.COLOR_RGB2BGR) # perform preprocessing (crop, resize etc.) image_array = preprocess(image_array) # add singleton batch dimension image_array = np.expand_dims(image_array, axis=0) steering_angle = float(model.predict(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 compute(dat,lab): BATCH_SIZE = 100 img = tf.Variable(np.zeros((BATCH_SIZE,IMAGE_SIZE,IMAGE_SIZE,NUM_CHANNELS),dtype=np.float32)) out = model(img) r = [] for i in range(0,len(dat),BATCH_SIZE): data = img o = s.run(out, feed_dict={img: preprocess(dat[i:i+BATCH_SIZE])}) r.extend(o) print(np.mean(np.abs(r)),np.std(np.abs(r)))
def compute(dat, lab): BATCH_SIZE = 100 img = tf.Variable( np.zeros((BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS), dtype=np.float32)) out = model(img) r = [] for i in range(0, len(dat), BATCH_SIZE): data = img o = s.run(out, feed_dict={img: preprocess(dat[i:i + BATCH_SIZE])}) r.extend(o) print(np.mean(np.abs(r)), np.std(np.abs(r)))
def train_and_predict(): print('Creating model...') model = get_model(int(img_rows/5), int(img_cols/5)) print('Done') print('-') print('Loading and preprocessing data...') imgs_train, imgs_train_mask = load_data('data/train') print('preprocessing...') imgs_train = preprocess(imgs_train) imgs_train_mask = preprocess(imgs_train_mask) imgs_train = imgs_train.astype('float32') imgs_train_mask = imgs_train_mask.astype('float32') print('Done.') print('-') print('Training model...') print('-') model.fit(imgs_train, imgs_train_mask, batch_size=16, epochs=50, verbose=1, shuffle=True, validation_split=0.2) print('Done') print('-') print('Saving model as model.h5') model.save('model.h5') print('Done saving.')
def predict(folder_name, mode): print_heading('Loading and preprocessing data...') image_rows = 600 image_cols = 800 folder_pattern = os.path.join(folder_name, '*.jpg') image_list = glob.glob(folder_pattern) total = int(len(image_list) / miss) + 1 if miss > 0 else len(image_list) imgs = np.ndarray((total, image_rows, image_cols, 3), dtype=np.uint8) imgs_id = np.ndarray((total, ), dtype=object) count = 0 for image_path in image_list: image_name = image_path.split('\\')[-1] if miss == 0 or count % miss == 1: index = int(count / miss) if miss > 0 else count imgs[index] = np.array( [imread(os.path.join(folder_name, image_name))]) imgs_id[index] = image_name.split('.')[0] count += 1 imgs = preprocess(imgs).astype('float32') if mode == "carla": mean = np.mean(imgs) # mean for data centering std = np.std(imgs) # std for data normalization imgs -= mean imgs /= std print_heading('Loading saved weights...') model = get_unet(mode) model.load_weights(os.path.join(mode, 'tl_weights.h5')) print_heading('Predicting masks on test data...') imgs_mask = model.predict(imgs, verbose=1) print_heading('Saving predicted masks to files...') pred_dir = os.path.join(folder_name, 'preds') if not os.path.exists(pred_dir): os.mkdir(pred_dir) for image, image_id in zip(imgs_mask, imgs_id): image = (image[:, :, 0] * 255.).astype(np.uint8) imsave(os.path.join(pred_dir, image_id + '.pred.png'), image)
def exp(regressor, shuffle=True, randomSeeds=range(0,1), train_pctl=0.7, train_vis=True, save=True, pca_n=50, **regressor_param): df = preprocess() train_r2s = [] val_r2s = [] aucs = [] for seed in randomSeeds: train, validation = shuffle_dataframe(dataframe=df, shuffle=shuffle, seed=seed, pctl=train_pctl) train, validation = apply_pca(train, validation, n_components=pca_n) model, train_r2, val_r2, auc = regression_model(regressor, train, validation, visual=train_vis, **regressor_param) train_r2s.append(train_r2) val_r2s.append(val_r2) aucs.append(auc) #print(train_r2s) train_r2_mean = np.mean(train_r2s) print("train r squared mean: ") print(train_r2_mean) #print(val_r2s) val_r2_mean = np.mean(val_r2s) print("validation r squared mean: ") print(val_r2_mean) #print(aucs) auc_mean = np.mean(aucs) print("AUC mean: ") print(auc_mean) if save: res = {"seeds": randomSeeds, "train_r2" : train_r2s, "val_r2" : val_r2s, "auc" : aucs} df = pd.DataFrame(res, columns=["seeds", "train_r2", "val_r2", "auc"]) #save to csv file df.loc["mean"] = df.mean() df.to_csv("exp_results_seeds.csv") return train_r2_mean, val_r2_mean, auc_mean
def learn(): if request.method == "POST": username = str(request.form['username']) data = pd.read_json(request.json, orient='split', convert_dates=['time']) print(data.head) if not os.path.isdir(username): os.mkdir(username) # data.set_index('time', inplace=True) train, test = model.datasplit(data) x_train, x_test, num_features = model.preprocess(train, test, username) model.train_net(x_train, train, num_features, username) return "Model successfully trained!"
def predict(input_or_path: Union[str, pd.DataFrame]) -> List[int]: """Make predictions for new survey responses""" if isinstance(input_or_path, str): X_df = load_new_survey_responses(input_or_path) else: X_df = input_or_path.copy() if not os.path.exists('./model.joblib') \ or not os.path.exists('./encoders.joblib'): model, encoders = train() else: model = joblib.load('./model.joblib') encoders = joblib.load('./encoders.joblib') X, _, _ = preprocess(X_df, None, encoders=encoders) return safe_predict(model, X)
def socket_predict(message): text = model.preprocess(message['data']) annotations = model.predict(text) annotations[0] = annotations[0][1: -1] # remove first and last [CLS] + [SEP] text_tokens, labels = model.align_tokenization_with_labels( text.split(), annotations) print(text_tokens) print(labels) ex = model.generate_sens_viz_cont(text_tokens[0], labels[0][:len(labels[0])]) ex['settings'] = {} html = displacy.render(ex, style='ent', manual=True, options={'colors': VIZ_COLOR_OPTIONS}) emit('annotations', {'data': html})
def run_segmentation(img_name, defect_type, switches, opacity): # Retrieve image and run model im = img_map[img_name] defects = model.predict(preprocess(im), batch_size=1)[0] # Generate the figures fig_segment = px.imshow(defects[:, :, defect_type], title='Segmentation Map (U-Net)') fig_im = px.imshow(im, color_continuous_scale='gray', title='Original image') fig_im.update_layout(coloraxis_showscale=False) if 'overlay' in switches: fig_im.add_trace( go.Heatmap(z=defects[:, :, defect_type], opacity=opacity)) fig_im.update_layout(title='Original image (with overlay)') return fig_im, fig_segment
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 = preprocess(np.asarray(image)) transformed_image_array = image_array[None, :, :, :] steering_angle = float( model.predict(transformed_image_array, batch_size=1)) / 4 throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
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) 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)) # Use in track 2: set throttle inversally proportional of speed. Also reduce throttle when steering angle increases. # throttle = min(1, (6 - 5*abs(float(steering_angle)))/max(1, float(speed))) throttle = 0.2 print(steering_angle, throttle) send_control(steering_angle, throttle)
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) # Preprocess image before passing to model for prediction height = image_array.shape[0] width = image_array.shape[1] image_array = image_array[height // 2 - 25:height - 25, 50:width - 50] image_array = preprocess(image_array) # Preprocess finished 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.2 # Reduced speed while turning for older mac if abs(steering_angle) > 0.1 and float(speed) > 5.0: throttle = 0.1 # Increased throttle for tough terrain on track 2 if float(speed) < 5.0: throttle = 0.4 print(steering_angle, throttle) send_control(steering_angle, throttle)
def predict(): model = load_model() files = request.files['images'] image = read_image(files) image = preprocess(image) # response = np.array_str(np.argmax(out,axis=1)) predicted = model(image) value = make_predictions(predicted) maxArg = tf.argmax(value, axis=1) print(maxArg) maxarg = maxArg.numpy() print("------------------") print(value) print("------------------") response = { 'target': np.array_str(value.numpy()), 'Result': f"The dress is {class_names[maxarg[0]]}." } return response
def exp(regressor, pca_n_components=None, **regressor_param): df = preprocess() df = df.iloc[:, 1:] #print(df.head()) sample_num = df.shape[0] val_pred = [] for row in range(sample_num): val = df.iloc[[row]] train = df.drop([row]) if pca_n_components != None: train, val = apply_pca(train, val, n_components=pca_n_components) model = fit(regressor, train, **regressor_param) pred = predict(model, val) val_pred.append(pred[0]) real_label = df.iloc[:, 0].values val_pred = np.array(val_pred) r2 = pearson_r_square(val_pred, real_label) print("pearson r square: {}".format(r2)) roc_data = calculate_SSA(val_pred, real_label, 5, 2, 10, 0.1) auc = np.trapz(roc_data[::-1, 1], roc_data[::-1, 2]) print("AUC: {}".format(auc)) plot_corr2(val_pred, real_label, r2, auc, roc_data)
fname = os.path.join("./raw_data", str(uuid.uuid4()) + ".png") cv2.imwrite(fname, image) bad_count = 0 avg_prediction = [] frame_array = [] while (True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here small = cv2.resize(frame, (96, 96)) array = preprocess(np.expand_dims(small, 0)) # Display the resulting frame prediction = np.squeeze(model.predict_on_batch(array)[0]) avg_prediction.append(prediction) frame_array.append(small) if len(avg_prediction) > 10: avg_prediction = avg_prediction[1:] frame_array = frame_array[1:] else: continue test_value = np.mean(prediction) print(test_value, end='\r')
import pandas as pd from model import preprocess, train, predict df = pd.read_csv("../data/train.csv", sep=";", decimal=",", index_col=[1, 2, 3]) df_train = preprocess(df) m = train(df_train) df = pd.read_csv("../data/test.csv", sep=";", decimal=",", index_col=[1, 2, 3]) df_test = preprocess(df, train=False) df_test["predicted"] = predict(m, df_test) print(df_test)
def train_and_predict(parent_folder): print_heading('Loading and preprocessing train data...') imgs_train, imgs_mask_train = load_train_data(parent_folder) imgs_train = preprocess(imgs_train) imgs_mask_train = preprocess(imgs_mask_train) imgs_train = imgs_train.astype('float32') mean = np.mean(imgs_train) # mean for data centering std = np.std(imgs_train) # std for data normalization if parent_folder == "carla": imgs_train -= mean imgs_train /= std imgs_mask_train = imgs_mask_train.astype('float32') imgs_mask_train /= 255. # scale masks to [0, 1] print_heading('Creating and compiling model...') model = get_unet(parent_folder) model_checkpoint = ModelCheckpoint('tl_weights.h5', monitor='val_loss', save_best_only=True) print_heading('Fitting model...') model.fit(imgs_train, imgs_mask_train, batch_size=16, epochs=30, verbose=1, shuffle=True, validation_split=0.2, callbacks=[model_checkpoint]) print_heading('Fitting model finished') print_heading('Loading and preprocessing test data...') imgs_test, imgs_id_test = load_test_data(parent_folder) imgs_test = preprocess(imgs_test) imgs_test = imgs_test.astype('float32') if parent_folder == "carla": imgs_test -= mean imgs_test /= std print_heading('Loading saved weights...') model.load_weights('tl_weights.h5') print_heading('Predicting masks on test data...') imgs_mask_test = model.predict(imgs_test, verbose=1) print_heading('Saving predicted masks to files...') pred_dir = 'preds' if not os.path.exists(pred_dir): os.mkdir(pred_dir) for image, image_id in zip(imgs_mask_test, imgs_id_test): image = (image[:, :, 0] * 255.).astype(np.uint8) imsave(os.path.join(pred_dir, image_id + '.pred.png'), image)
root = '/content/drive/MyDrive/Colab Notebooks/', download = True, train = True, transform = transforms.ToTensor() ) # Reorganizing Data train_dict = {i : MNIST_train.data[MNIST_train.targets == i].unsqueeze(1) / 255 for i in range(10)} print(*[f'{imgs.shape[0]} images of Label {label} of shape {imgs.shape[1:]}' for label, imgs in train_dict.items()], sep='\n') # for i in range(10): # train_dict[i] = MNIST_train.data[MNIST.targets == i] # One Sample Image from each class IMAGES = {0:train_dict[0][95], 1:train_dict[1][1], 2:train_dict[2][2], 3:train_dict[3][0]} for i in [4,6,7,8]: IMAGES[i] = train_dict[i][1] IMAGES[5] = train_dict[5][63] IMAGES[9] = train_dict[9][5] # Visualizing Sample images GRID = torchvision.utils.make_grid(preprocess(torch.stack([*IMAGES.values()])), nrow=5) plt.figure(figsize=(15,5)) plt.imshow(np.transpose(GRID, (1,2,0))) # Training only on Images of '0','1','2' zeros_images = train_dict[0][:100] ones_images = train_dict[1][:100] twos_images = train_dict[2][:100] train_images = torch.stack([zeros_images, ones_images, twos_images], dim=1) # train_images.shape # train_images[:,0,...].shape = torch.Size([100, 1, 28, 28])
from model import preprocess, shuffle_dataframe, apply_pca import numpy as np import pandas as pd import matplotlib.pyplot as plt if __name__ == "__main__": df = preprocess() train, val = shuffle_dataframe(dataframe=df, shuffle=True, seed=2, pctl=0.7) train, val = apply_pca(train, val, n_components=2) #print(train.head(), validation.head()) train_pos = train[train.iloc[:, 0] >= 5] train_neg = train[train.iloc[:, 0] < 5] val_pos = val[val.iloc[:, 0] >= 5] val_neg = val[val.iloc[:, 0] < 5] #print(train_pos.head(), val_neg.head()) plt.subplot(1, 2, 1) plt.xlabel('pca_component_1', fontsize=12) plt.ylabel('pca_component_2', fontsize=12) #plt.ylim((0,1)) plt.scatter(train_pos.iloc[:, 1], train_pos.iloc[:, 2], c='b', label="potent") plt.scatter(train_neg.iloc[:, 1], train_neg.iloc[:, 2], c='r',
def controller(data): steering = sess.run(pred, feed_dict = { X: [model.preprocess(base64_to_cvmat(data["image"]), config.INPUT_IMAGE_CROP)], keep_prob: 1.0})[0] throttle = controller.speed_controller.update(float(data["speed"])) return steering, throttle