def inference(sess, imgs, input_shape, input_ts, output_ts, keep_prob_ts, label_colors, *, is_rgb=False, set_black_background=False): """Infer test images :param sess: TF Session :param imgs: a list of numpy.ndarray, (w, h, c) List of image data. :param input_shape: tuple Shape of the input image :param input_ts: TF Tensor Inputs. :param output_ts: TF Tensor Outputs. :param keep_prob_ts: TF Tensor Keep probability. :param label_colors: an array of tuple Colors for different labels (classes). :param is_rgb: bool True for RGB label image; False for GBR label image. :param set_black_background: bool True for use (0, 0, 0) as the background color for decoding. :return decoded_masks: a list numpy.ndarray, (w, h, c) A list of Label images with each class represented by a different color. """ X, _ = preprocess_data(np.array(imgs), None, None, input_shape=input_shape, is_training=False, is_rgb=False) outputs = sess.run(tf.nn.softmax(output_ts), feed_dict={ keep_prob_ts: 1.0, input_ts: X }) decoded_masks = [] for output in outputs: mask = cv2.resize(output, (imgs[0].shape[1], imgs[0].shape[0])) mask = np.argmax(mask, axis=2) decoded_masks.append( decoding_mask(mask, label_colors, is_rgb=is_rgb, set_black_background=set_black_background)) return decoded_masks
def test_data_processing(self): target_shape = (320, 480) images, labels = preprocess_data(self.images_test, self.labels_test, self.label_colors, input_shape=target_shape) self.assertEqual(images.shape[0], len(self.images_test)) self.assertEqual(labels.shape[0], len(self.labels_test)) for X, Y in zip(images, labels): self.assertLessEqual(-1, X.min()) self.assertGreaterEqual(1, X.max()) self.assertEqual(X.shape, (*target_shape, 3)) self.assertEqual( Y.shape, (target_shape[0], target_shape[1], self.n_classes)) self.assertEqual(sorted(np.unique(Y)), [0, 1])
def main(file_path, token_name, rating_model, recom_model): data = pd.read_csv(file_path, encoding='utf-8') pd_df = data.replace(np.nan, '', regex=True) pd_df, other_data = cleaning.get_one_hot_encode_for_model(pd_df, train=False) pd_df = cleaning.preprocess_data(pd_df, train=False, tokenizer_name=token_name) pd_df = np.hstack((pd_df, other_data)) rating_model = load_model(rating_model) recommendation_model = load_model(recom_model) ratings = rating_model.predict(pd_df, verbose=1) recommendations = recommendation_model.predict(pd_df, verbose=1) rating = [x.index(max(x)) for x in ratings.tolist()] recommendations = [x.index(max(x)) for x in recommendations.tolist()] path = 'prediction' with open (os.path.join(path,"rating.txt"),"w")as fp: fp.write("\n".join(rating)) with open (os.path.join(path,"recommendations.txt"),"w")as fp: fp.write("\n".join(recommendations))
def get_frame(path): matrix = preprocess_data(path) label_matrix(matrix) return pd.DataFrame(matrix, columns=COLUMNS)
print('Found %d files' % num_input_files) print('Writing output to %s\n' % output_path) seq2seq = load_model() # Process each file. print('Processing...') num_success = 0 for i, filename in enumerate(input_files): if i and i % 10 == 0: print('Processed %d of %d' % (i, num_input_files)) try: # Load data data, end_time, channel = read_input_file(filename) data_processed = preprocess_data(data, INPUT_TIMESTEPS) # Predict. seq = seq2seq(np.expand_dims(data_processed, axis=0)) # Write to disk. filebase, _ = os.path.splitext(os.path.basename(filename)) seq_to_csv(seq, os.path.join(output_path, filebase + '_continued.csv'), start_time=end_time, channel=channel) num_success += 1 except Exception as e: print('ERROR: Problem processing file %s.' % filename) print('Exception: %s' % str(e))
def main(file_path, token_name, rating_epc, recom_epc, batch_s, rating_m, recom_m): data = pd.read_csv(file_path, encoding='utf-8') data = data.replace(np.nan, '', regex=True) pd_rating, pd_recom = cleaning.get_one_hot_encoded( data['Rating']), cleaning.get_one_hot_encoded(data['Recommended IND']) data, other_data = cleaning.get_one_hot_encode_for_model(data, train=True) data = data.drop(['Rating', 'Recommended IND'], axis=1) pd_df_train, pd_df_test, rating_train, rating_test, recommendation_train, recommendation_test, other_data_train, \ other_data_test = train_test_split(data, pd_rating, pd_recom, other_data,test_size=0.2, random_state=25) pd_df_train, vocabSize = cleaning.preprocess_data( pd_df_train, train=True, tokenizer_name=token_name) pd_df_test = cleaning.preprocess_data(pd_df_test, train=False, tokenizer_name=token_name) pd_df_train = np.hstack((pd_df_train, other_data_train)) pd_df_test = np.hstack((pd_df_test, other_data_test)) total_input_size = pd_df_train.shape[1] rating_model = get_nlp_model(vocabSize, total_input_size, total_input_size - 60, rating_train.shape[1], 300) recommendation_model = get_nlp_model(vocabSize, total_input_size, total_input_size - 60, recommendation_train.shape[1], 300) plot_model(rating_model, to_file='rating_model.png', show_shapes=True, show_layer_names=True) plot_model(recommendation_model, to_file='recommendation_mode.png', show_shapes=True, show_layer_names=True) # Train the rating and recommendation model using training set. rating_model.fit(pd_df_train, rating_train, validation_data=(pd_df_test, rating_test), epochs=rating_epc, batch_size=batch_s, verbose=1) recommendation_model.fit(pd_df_train, recommendation_train, validation_data=(pd_df_test, recommendation_test), epochs=recom_epc, batch_size=batch_s, verbose=1) # Evaluate model on the test set and print results rating_scores = rating_model.evaluate(pd_df_test, rating_test, verbose=1) print("Rating Model CNN Error: %.2f%%" % (100 - rating_scores[1] * 100)) recommendation_score = recommendation_model.evaluate(pd_df_test, recommendation_test, verbose=1) print("Recommendation Model CNN Error: %.2f%%" % (100 - recommendation_score[1] * 100)) # Save the trained model. rating_model.save(rating_m) recommendation_model.save(recom_m)