def predict(self): parser = argparse.ArgumentParser( description='Audio Classification Training') parser.add_argument('--model_fn', type=str, default='models/lstm.h5', help='model file to make predictions') parser.add_argument('--pred_fn', type=str, default='y_pred', help='fn to write predictions in logs dir') parser.add_argument('--dt', type=float, default=1.0, help='time in seconds to sample audio') parser.add_argument('--sr', type=int, default=16000, help='sample rate of clean audio') parser.add_argument('--threshold', type=str, default=20, help='threshold magnitude for np.int16 dtype') args, _ = parser.parse_known_args() predict.make_prediction(args)
def main(): st.title('Baq House Predictor') description() params = get_params() yhat = int(make_prediction(params)[0]) st.subheader(f"Estimated Rent Price: ${yhat:,}") credit()
def predict(): company_name = request.form['param'] score, proba = make_prediction(company_name) if score is not None and proba is not None: return json.dumps({"score": score, "proba": proba}) else: return json.dumps({"score": None, "proba": None})
def predict(): if request.method == 'POST': # Step 1: Extract POST data from request body as JSON json_data = request.get_json() # _logger.debug(f'Inputs: {json_data}') # Step 2: Validate the input using marshmallow schema # input_data, errors = validate_inputs(input_data=json_data) #print(input_data) # Step 3: Model prediction result = make_prediction(input_data=json_data) # _logger.debug(f'Outputs: {result}') # Step 4: Convert numpy ndarray to list predictions = result.get('predictions') # version = result.get('version') # patient_id = result.get('patient_id') # Step 5: Return the response as JSON return jsonify({ 'predictions': predictions, # 'version': version, # 'patient_id':patient_id })
def test_make_single_prediction(): data = pd.read_csv(config.TRAINING_DATA_FILE) drop_duplicate = pp.DropDuplicateValues() data = drop_duplicate.fit_transform(data) drop_missing = pp.DropMissingValues() data = drop_missing.fit_transform(data) create_polarity = pp.CreatePolarityFeature() data = create_polarity.fit_transform(data) X_train, X_test, y_train, y_test = train_test_split( data[config.INPUT_FEATURES], data[config.TARGET], test_size=0.3, random_state=42) # Given single_test_json = X_test[0:1].to_json(orient='records') # When subject = make_prediction(input_data=single_test_json) # Then assert subject is not None
def opt(x): soundfile.write('temp/temp.wav', y, sr, subtype='PCM_16') parser = argparse.ArgumentParser(description='Audio Classification Training') parser.add_argument('--model_fn', type=str, default='models/lstm.h5', help='model file to make predictions') parser.add_argument('--pred_fn', type=str, default='y_pred', help='fn to write predictions in logs dir') parser.add_argument('--dt', type=float, default=1.0, help='time in seconds to sample audio') parser.add_argument('--sr', type=int, default=16000, help='sample rate of clean audio') parser.add_argument('--threshold', type=str, default=20, help='threshold magnitude for np.int16 dtype') args, _ = parser.parse_known_args() weights0 = predict.make_prediction(args) y1 = np.copy(y) for _ in range(x[0]): y1 = methods.reduce_noise_power(y1, sr) for _ in range(x[1]): y1 = methods.reduce_noise_centroid_s(y1, sr) for _ in range(x[2]): y1 = methods.reduce_noise_centroid_mb(y1, sr) for _ in range(x[3]): y1 = methods.reduce_noise_median(y1, sr) soundfile.write('temp/temp.wav', y1, sr, subtype='PCM_16') parser = argparse.ArgumentParser(description='Audio Classification Training') parser.add_argument('--model_fn', type=str, default='models/lstm.h5', help='model file to make predictions') parser.add_argument('--pred_fn', type=str, default='y_pred', help='fn to write predictions in logs dir') parser.add_argument('--dt', type=float, default=1.0, help='time in seconds to sample audio') parser.add_argument('--sr', type=int, default=16000, help='sample rate of clean audio') parser.add_argument('--threshold', type=str, default=20, help='threshold magnitude for np.int16 dtype') args, _ = parser.parse_known_args() weights1 = predict.make_prediction(args) return max(weights0) - max(weights1)
def do_test(model_ckpt_path, test_data_path, result_path, word_dict_path, emb_dim, hid_dim): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') word_dict = load_pkl(word_dict_path) PAD_IDX = word_dict.word2idx("<PAD>") print("load data...") testData = SentenceDataset(load_pkl(test_data_path), word_dict, PAD_IDX, training=False) print("load model...") model = get_model(word_dict.get_len(), word_dict.get_len(), emb_dim, hid_dim, device) model.load_state_dict(torch.load(model_ckpt_path)) model.to(device) print("predicting...") make_prediction(model, testData, word_dict, result_path, device)
def home(): form = PredictForm() # form = YesNoQuestionForm() if form.validate_on_submit(): flash("making prediction for the data given...") dictionary = make_dictionary(form) output = make_prediction(dictionary) text = "Predicted price is {}".format(output) return render_template("home.html", form=form, prediction_text=text) return render_template("home.html", form=form)
def upload(): if request.method == 'POST': profile = request.files['images'] profile.save(os.path.join(uploads_dir, profile.filename)) print("Image saved to the folder") print("The filename is ", profile.filename) prediction = make_prediction(profile.filename) answer['prediction'] = prediction print("prediction made") return "500"
def results(): if not request.json: abort(400) data = request.get_json(force=True) if not validate_json(data): abort(422) prediction = make_prediction(data) return jsonify(prediction)
def test_make_single_prediction(): # Given test_data = load_dataset(file_name='test.csv') single_test_input = test_data[0:1] # When subject = make_prediction(input_data=single_test_input) # Then assert subject is not None assert isinstance(subject.get('predictions')[0], float) assert math.ceil(subject.get('predictions')[0]) == 112476
def test_predictions(): '''Test if the metric are calculated correctly''' from predict import make_prediction test_data_dir = 'src/tests/test_target.csv' expected = [ 0.32, 0.10526315789473684, 0.10526315789473684, 0.10526315789473684 ] out = make_prediction('src/tests/test_model.pkl', test_data_dir, False) assert out == expected
def test_make_prediction(): time = datetime.datetime(2000, 01, 01, 00, 00, 00) in_ = {'mean': 600, 'std': 0} out = { 'std': 0, 'upper99': 600, 'prediction': datetime.datetime(2000, 1, 1, 0, 10), 'latest': datetime.datetime(2000, 1, 1, 0, 10), 'lower99': 600, 'earliest': datetime.datetime(2000, 1, 1, 0, 10), 'mean': 600 } eq_(predict.make_prediction(in_, time), out) in_ = {'mean': 900, 'std': 300} out = { 'std': 300, 'upper99': 1800, 'prediction': datetime.datetime(2000, 1, 1, 0, 15), 'latest': datetime.datetime(2000, 1, 1, 0, 30), 'lower99': 0, 'earliest': datetime.datetime(2000, 1, 1, 0, 0), 'mean': 900 } eq_(predict.make_prediction(in_, time), out)
def test_make_multiple_predictions(): # Given test_data = load_dataset(file_name='test.csv') original_data_length = len(test_data) multiple_test_input = test_data # When subject = make_prediction(input_data=multiple_test_input) # Then assert subject is not None assert len(subject.get('predictions')) == 1451 # We expect some rows to be filtered out assert len(subject.get('predictions')) != original_data_length
def test_predicted_df(): '''Test if the DF saved after predictions contains the predictions''' from predict import make_prediction test_data_dir = 'src/tests/test_target.csv' _ = make_prediction('src/tests/test_model.pkl', test_data_dir, False, save_to_file='src/tests/test_pred_out.csv') pred_out = pd.read_csv('src/tests/test_pred_out.csv', sep=';') errors = [] if "Predicted" not in pred_out.columns.sort_values().tolist(): errors.append('Predicted column not in DF.') if pred_out.Predicted.isna().any(): errors.append('Found Nan in Predicted values') os.remove('src/tests/test_pred_out.csv') assert not errors, "errors occured:\n{}".format("\n".join(errors))
def predict(): tweet = request.args.get('tweet', None) if tweet == None: return redirect(url_for('home')) prediction = make_prediction(tweet) return render_template('predict.html', prediction_text=prediction)
def example(): # get face count of the group face_count = get_face_count(group_similarity_src) # select from database print("\n\n\n############ Training Session ############") datas = [] for cnn_build_time_range in cnn_build_time_range_set: start_time = cnn_build_time_range[0] end_time = cnn_build_time_range[1] this_datas = get_data(start_time, end_time) datas = datas + this_datas results.clear() # print import data result for banchmark print("\n\n\n------ Original Result ------") find_rate(datas, 'person_first_id') # build automaton root_c = build_model(datas) # apply decision model matched_face_table, unmatched_face_table, unmatched_validate_table, unmatched_face_objects = find_match( root_c.next, face_count) # convert data to vector for neuron network unmatched_face_table, unmatched_validate_table, unmatched_face_objects = convert_to_vector( matched_face_table, unmatched_face_table, unmatched_validate_table, unmatched_face_objects) # separate training and test data division = int(unmatched_face_table.shape[0] * 0.7) X = unmatched_face_table[:division] y = unmatched_validate_table[:division] X_test = unmatched_face_table[division:] y_test = unmatched_validate_table[division:] # build cnn, and save the model model = conv_net(X, y, X_test, y_test) # use the model to make prediction on the construct session print("\n\n\n------ Unmatched Face (Corrected by CNN) ------") cnn_result = make_prediction(model, unmatched_face_table, unmatched_face_objects) print("\n\n\n------ Overall Result (Corrected by CNN) ------") all_result = list(results) + list(cnn_result) find_rate(all_result, 'result_id') print(predict_time_range_set) for predict_time_range in predict_time_range_set: print(predict_time_range) # get data from different session for testing print("\n\n\n############ Indipendent Testing Session ############") start_time = predict_time_range[0] end_time = predict_time_range[1] datas = get_data(start_time, end_time) results.clear() # print import data result for banchmark print("\n\n\n------ Original Result ------") find_rate(datas, 'person_first_id') # build automaton root_c = build_model(datas) # apply decision model matched_face_table, unmatched_face_table, unmatched_validate_table, unmatched_face_objects = find_match( root_c.next, face_count) # convert data to vector for neuron network unmatched_face_table, unmatched_validate_table, unmatched_face_objects = convert_to_vector( matched_face_table, unmatched_face_table, unmatched_validate_table, unmatched_face_objects) # evaluate the model using data of the indipendent test session score = model.evaluate(unmatched_face_table, unmatched_validate_table) print("\nCross Session Accuracy: ", score[-1]) # use the model to make prediction on the indipendent test session print("\n\n\n------ Unmatched Face ------") find_rate(unmatched_face_objects, 'person_first_id') print("\n\n\n------ Unmatched Face (Corrected by CNN) ------") cnn_result = make_prediction(model, unmatched_face_table, unmatched_face_objects) print("\n\n\n------ Overall Result (Corrected by CNN) ------") all_result = list(results) + list(cnn_result) find_rate(all_result, 'result_id')
# Data preprocessing prepare_features('data/train.csv', 'data/train_processed.csv', force_overwrite) prepare_features( 'data/val.csv', 'data/test_processed.csv', force_overwrite) # NOTE: we rename val to test as it makes more sense # Train and test each model 5 times and report average scores metrics_df = pd.DataFrame(columns=['Accuracy', 'Precision', 'Recall', 'F1']) for model in tqdm(models): model_runs = [] for run in range(runs_per_model): train_model('data/train_processed.csv', model=model, standardize=standardize, verbose=False) run_result = make_prediction('data/' + model + '.pkl', 'data/test_processed.csv', standardize=standardize) model_runs.append(run_result) model_runs = pd.DataFrame( model_runs, columns=['Accuracy', 'Precision', 'Recall', 'F1']).mean().rename(model) metrics_df = metrics_df.append(model_runs) metrics_df = metrics_df.round(3) print('\n Final metrics: \n', metrics_df)
def main(argv): df_train = clean_data(argv[0]) df_test = clean_data(argv[1]) make_prediction(df_train, df_test, fit_linear_model, './output/prediction_logit.csv')
from predict import make_prediction answer = {} prediction = make_prediction('image_picker8338892262053686856.jpg') answer['prediction'] = prediction print(answer)
def main(argv): df_train = clean_data(argv[0]) df_test = clean_data(argv[1]) make_prediction(df_train, df_test, fit_tree, './output/prediction_tree.csv')
main_dir = os.getcwd() output_dir = os.path.join(main_dir, 'output') # construct the argument parse and parse the arguments parser = argparse.ArgumentParser() parser.add_argument("-train", action='store_true', help="trains the model") parser.add_argument("-predict", "--predict", required=False, help="path to file") args = vars(parser.parse_args()) # for training if args['train']: import train train.train_model() # for prediction if args['predict']: image_path = args['predict'] # check for a valid image path if os.path.isfile(image_path): import predict predict.make_prediction(image_path, output_dir) else: print('Please provide a valid image path')
from predict import make_prediction assert make_prediction(['hate horrible' ]) == 'The tweet has a negative sentiment' assert make_prediction(['happy love roses happy' ]) == 'The tweet has a positive sentiment' assert make_prediction(['I love life']) == 'The tweet has a positive sentiment' assert make_prediction(['Your service was horrible, never again' ]) == 'The tweet has a negative sentiment'