def train(): form = TrainForm() if form.validate_on_submit(): classifier.train([form.data['example']], [form.data['label']]) flash('Thanks!', 'success') else: flash('An error occurred!', 'danger') return redirect('/')
def trainui(): form = TrainForm() if form.validate_on_submit(): print(request.form.to_dict()) args = copy.deepcopy(request.form.to_dict()) del args['csrf_token'] print(args['args']) generate_yaml('train.yaml', 'test.yaml', args) return redirect(url_for('trainImage', **args)) return render_template('train.html', title='nexo', form=form)
def home(): data1 = pd.read_csv('data.csv') data1 = data1[data1.label != 'nu'] x = data1.iloc[:, 0].values y = data1.iloc[:, 1].values vectorizer = TfidfVectorizer(encoding='utf-8') x = vectorizer.fit_transform(data1['text']) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=50) model = svm.SVC(gamma='auto', probability=True) model.fit(x_train, y_train) x_score = model.score(x_train, y_train) y_score = model.score(x_test, y_test) sform = SearchForm() # pos = 10 # neg = 18 if sform.validate_on_submit(): sent = sform.text.data sent1 = np.array(list(sent)) se = vectorizer.transform(sent1) pre = model.predict_proba(se) pos = pre[0][0] * 100 neg = pre[0][1] * 100 flash(f'Training accuracy:{x_score}, and Testing accuracy: {y_score}', 'success') return render_template('search.html', form=sform, pos=pos, neg=neg) else: return render_template('home.html', form=sform) tform = TrainForm() if tform.validate_on_submit(): flash('Training in progress', 'success') return render_template('search.html', form='tform') else: flash('You should first train your model', 'danger')
def add_train(): if current_user.sostav != 'P': return redirect('/') else: form = TrainForm() if form.validate_on_submit(): session = db_session.create_session() fdate = datetime.datetime.combine(form.date.data.date(), form.time.data.time()) if session.query(Train).filter( Train.date == fdate, Train.sostav == form.sostav.data).first(): return render_template( 'add_train.html', form=form, message="У этого состава уже есть тренировка в это время") train = Train(date=fdate, sostav=form.sostav.data, dur=int(form.dur.data), user_id=int(current_user.id)) session.add(train) session.commit() return redirect('/success') return render_template('add_train.html', form=form)
def home(): form = PredictForm() if form.validate_on_submit(): cleaned = pattern.sub(' ', form.example.data.lower()) new_examples = [cleaned] predictions, probs = classifier.predict(new_examples) return render_template('result.html', max_coef=classifier.get_max_coefficient(), words=classifier.get_coefficients_for( new_examples[0]), example=new_examples[0], pos_prob=probs[1], neg_prob=probs[0], form=TrainForm()) return render_template('index.html', form=form)
def index(): trainForm = TrainForm() testForm = TestForm() if trainForm.submitTrain.data and trainForm.validate_on_submit(): if request.method == "POST": if os.path.exists('temp'): directoryUtils.rmtree("temp") os.mkdir('temp') for f in trainForm.images.data: # remove underscores to avoid ambiguity # as secure_filename() replaces os.path.sep with underscore safe_name = f.filename.replace('_', '') fil = secure_filename(safe_name) if allowed_file(fil, ['jpg', 'jpeg', 'png']): f.save(os.path.join('temp', fil)) split = float(trainForm.split.data) epochs = int(trainForm.epochs.data) if os.path.isfile(LOG_FILE_TRAIN): os.remove(LOG_FILE_TRAIN) w = open(LOG_FILE_TRAIN, 'w+') cmd = [ 'python', '-u', 'modelTrain.py', str(split), str(epochs), str(trainForm.width.data), str(trainForm.height.data) ] subprocess.Popen(cmd, stdout=w, stderr=subprocess.STDOUT) w.close() return redirect(url_for('trainResults')) if testForm.submitTest.data and testForm.validate_on_submit(): if request.method == "POST": if os.path.exists('temp'): directoryUtils.rmtree("temp") os.mkdir('temp') os.mkdir(os.path.join("temp", "model")) os.mkdir(os.path.join("temp", "label")) os.mkdir(os.path.join("temp", "images")) model = testForm.model.data label = testForm.labels.data if allowed_file(secure_filename(model.filename), ['h5']): model.save( os.path.join("temp", "model", secure_filename(model.filename))) else: return 'Invalid model.' if allowed_file(secure_filename(label.filename), ['dat']): label.save( os.path.join("temp", "label", secure_filename(label.filename))) else: return 'Invalid labels.' # process based on source print('Beginning handover to prediction script.') images_dir_path = None if testForm.source.data == 'images': for f in testForm.images.data: if allowed_file(secure_filename(f.filename), ['jpeg', 'jpg', 'png']): f.save( os.path.join('temp', 'images', secure_filename(f.filename))) images_dir_path = 'temp' elif testForm.source.data == 'path': images_dir_path = testForm.path.data if os.path.isfile(LOG_FILE_TEST): os.remove(LOG_FILE_TEST) w = open(LOG_FILE_TEST, 'w+') cmd = ['python', '-u', 'modelPredict.py', images_dir_path] subprocess.Popen(cmd, stdout=w, stderr=subprocess.STDOUT) w.close() return redirect(url_for('testResults')) return render_template("HTML_Interface.html", trainForm=trainForm, testForm=testForm)
def train(): # persist model # joblib.dump(clf, 'model.pkl') cm = [[0, 0], [0, 0]] score = 0 precision = 0 recall = 0 f1score = 0 form = TrainForm() if request.method == 'POST': # print(request.form['algorithm']) # read breast cancer data set breast_cancer_df = pd.read_csv('./static/breastcancer.csv') # if breast_cancer_df[""] # print(breast_cancer_df[breast_cancer_df["Class"]=='Benign']) mapping = {'Malignant': 1, 'Benign': 0} breast_cancer_df.replace({'Class': mapping}, inplace=True) X = breast_cancer_df.iloc[:, :-1].values y = breast_cancer_df.iloc[:, -1].values mask = X[:, 5] == '?' X[mask] = 'NaN' imputer = Imputer(missing_values='NaN', strategy='mean', axis=0) imputer = imputer.fit(X[:, 0:9]) X[:, 0:9] = imputer.transform(X[:, 0:9]) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0) if request.form['algorithm'] == 'nb': # # fit model clf = GaussianNB() elif request.form['algorithm'] == 'dt': # # fit model clf = DecisionTreeClassifier() elif request.form['algorithm'] == 'rf': # # fit model clf = RandomForestClassifier() # elif request.form['algorithm'] == 'svm': # # # fit model # clf = SVC() # elif request.form['algorithm'] == 'logreg': # # # fit model # clf = LogisticRegression() clf.fit(X_train, y_train) # persist model joblib.dump(clf, 'model.pkl') y_pred = clf.predict(X_test) cm = confusion_matrix(y_test, y_pred) # print(classification_report(y_test,y_pred)) score = clf.score(X_test, y_test) precision = precision_score(y_test, y_pred) recall = recall_score(y_test, y_pred) f1score = f1_score(y_test, y_pred) # test = [[5,1,1,1,2,1,3,1,1],[8,10,10,8,7,10,9,7,1]] # prediction = clf.predict_proba(test) # print(prediction) # flash(f'Model Created '+ str(request.form['algorithm']) + str(clf.score(X_test,y_test))+ str(cm),'success') flash(f'Model Created ' + str(request.form['algorithm']), 'success') # return redirect(url_for('train',cm = cm)) return render_template('train_model.html', title='Train Model', form=form, cm=cm, accuracy=score, precision=precision, recall=recall, f1score=f1score) return render_template('train_model.html', title='Train Model', form=form, cm=cm, accuracy=score, precision=precision, recall=recall, f1score=f1score)
def index(): trainForm = TrainForm() testForm = TestForm() if trainForm.submit.data and trainForm.validate_on_submit(): if request.method == "POST": if os.path.exists('temp'): directoryUtils.rmtree("temp") os.mkdir('temp') for f in trainForm.images.data: if allowed_file(secure_filename(f.filename), ['jpg', 'jpeg', 'png']): fil = secure_filename(f.filename) f.save(os.path.join('temp', fil)) split = float(trainForm.split.data) epochs = int(trainForm.epochs.data) print("Beginning handover to training script.") modelTrain.trainModel(split, epochs) modelList = [] query = re.compile(r'^(\w+)_(\d.\d{3})_(\d.\d{3}).h5$') for file in os.listdir("output"): matches = query.findall(file) model = {} if len(matches) > 0: model['specialization'] = "Maximum Accuracy" if matches[0][ 0] == "max_acc" else "Minimum Loss" model['val_loss'] = '{:.3f}'.format(float(matches[0][1])) model['val_acc'] = '{:.1f}%'.format( float(matches[0][2]) * 100) model['filename'] = file modelList.append(model) directoryUtils.rmtree("temp") return render_template("trainResults.html", models=modelList) if testForm.submit.data and testForm.validate_on_submit(): if request.method == "POST": if os.path.exists('temp'): directoryUtils.rmtree("temp") os.mkdir('temp') os.mkdir(os.path.join("temp", "model")) os.mkdir(os.path.join("temp", "label")) os.mkdir(os.path.join("temp", "images")) model = testForm.model.data label = testForm.labels.data if allowed_file(secure_filename(model.filename), ['h5']): model.save( os.path.join("temp", "model", secure_filename(model.filename))) else: return 'Invalid model.' if allowed_file(secure_filename(label.filename), ['dat']): label.save( os.path.join("temp", "label", secure_filename(label.filename))) else: return 'Invalid labels.' for f in testForm.images.data: if allowed_file(secure_filename(f.filename), ['jpeg', 'jpg', 'png']): f.save( os.path.join('temp', 'images', secure_filename(f.filename))) print("Beginning handover to prediction script.") modelPredict.predict() directoryUtils.rmtree("temp") return render_template("testResults.html") return render_template("HTML_Interface.html", trainForm=trainForm, testForm=testForm)