def run_iteration(iteration, hash_map): pst = PhaseStretchTransform() data = [] labels = [] #Finding all images images = [ os.path.join(root, name) for root, dirs, files in os.walk("../training_images") for name in files if name.endswith((".jpeg", ".jpg")) ] #Spliting it into training and testing groups training, testing = train_test_split(images, test_size=0.25) #Training Phase for imagePath in training: #Load the image, convert it to grayscale, and compute LBP image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if imagePath in hash_map: hist = hash_map[imagePath] else: hist = pst.compute(gray) hash_map[imagePath] = hist print str( iteration ) + " DEBUG(Training): Computed PST Histogram for " + imagePath #Extract the label from the image path, then update the label and data lists labels.append(imagePath.split("/")[-2]) data.append(hist) #Train classifier classifier = Classifier("Chi-Squared") print "\n\n" + str(iteration) + " DEBUG: Training Classifier" classifier.train(data, labels) print "\n\n" + str(iteration) + " DEBUG: Trained Classifier\n\n" #Testing Phase data = [] labels = [] for imagePath in testing: #Load the image, convert to grayscale, describe it and classify it image = cv2.imread(imagePath) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if imagePath in hash_map: hist = hash_map[imagePath] else: hist = pst.compute(gray) hash_map[imagePath] = hist print str( iteration ) + " DEBUG(Testing): Computed PST Histogram for " + imagePath data.append(hist) labels.append(imagePath.split("/")[-2]) print "\n\n" + str(iteration) + " DEBUG: Forming predictions" predictions = classifier.predict(data) counter = 0 print "\n\n" + str(iteration) + " DEBUG: Printing predictions\n\n" for index, prediction in enumerate(predictions): print "Name -> " + testing[index] + " Actual -> " + labels[ index] + " Prediction -> " + prediction if labels[index] == prediction: counter = counter + 1 accuracy = (float(counter) / float(len(predictions))) * 100.0 print "\n\n" + str(iteration) + " The Classifier Accuracy was " + str( accuracy) + "%" return accuracy
def main(): parser = argparse.ArgumentParser( description="Decode speech from parameter files.") parser.add_argument('-model_arch', '--model_arch', help='classifier file arch', required=True, default=None) parser.add_argument('-model_weights', '--model_weights', help='classifier file weights', required=True, default=None) parser.add_argument('-model_priori_proba_file', '--model_priori_proba_file', help='activations priori proba file', required=True, default=None) parser.add_argument('-fst_folder', '--fst_folder', help="folder containg HCLG graph", required=True, default=None) parser.add_argument('-acoustic_model_labels_file', '--acoustic_model_labels_file', help="Text file name containing input labels", required=True, default=None) parser.add_argument('-srcfile', '--srcfile', help='file containing wave files pathes to test on', required=True, default=None) parser.add_argument('-outfile', '--outfile', help='Filename to write output hypotheses', required=True, default=None) parser.add_argument('-reffile', '--reffile', help='Filename to write ref hypotheses', required=True, default=None) parser.add_argument('-amw', '--amw', help='Relative weight of LM score', required=False, type=float, default=9) parser.add_argument('-max_active_tokens', '--max_active_tokens', help='Maximum token count per frame', required=False, type=int, default=1000) parser.add_argument('-beam_width', '--beam_width', help='Maximum token count per frame', required=False, type=float, default=12.0) args = parser.parse_args() # decoder = Decoder(fst_folder, acoustic_model_labels_file) classifier = Classifier(args.model_arch, args.model_weights, args.model_priori_proba_file) all_time_start = time() predictedSents = [] with open(args.reffile, "w") as f: pass # clear file try: with open(args.srcfile, 'r') as ftest: with open(args.outfile, 'w', buffering=1) as fout: all_time_start = time() for wav_file in ftest.read().split(): features = np.transpose(np.array(wav_to_feat(wav_file))) activations = classifier.eval(features) with open( f"{wav_file.split('/')[-1].split('.')[-2]}_Ramy_activations.txt", "w") as f: f.write( f"{activations.shape[0]} {activations.shape[1]}\n") np.savetxt(f, activations) write_ref_output(wav_file) except KeyboardInterrupt: print("[CTRL+C detected]") text = '\n'.join(predictedSents) with open(args.outfile, "w") as f: f.write(text) print(f"total time takes {int(time()-all_time_start)} seconds")
from fastapi import FastAPI, File from classifier import Classifier app = FastAPI() app.model = Classifier() @app.get('/') def healthcheck(): return {'alive': True} @app.post('/files/') async def recognize_flower(file: bytes = File(...)): pred, pred_idx, probs = app.model.learner.predict(file) return {'prediction': pred, 'probability': float(probs[pred_idx])}
from flask import Flask from flask_restplus import Api, Resource from classifier import Classifier server = Flask(__name__) app = Api(app=server) name_space = app.namespace('analyzer', description='Social Sentiment Analyzer APIs') cl = Classifier() @name_space.route("/predict/<string:social_text>") class MainClass(Resource): def get(self, social_text): cleaned = cl.clean_text(social_text) vectorized = cl.vectorize_text(cleaned) prediction = cl.predict_text(vectorized) return {"prediction": prediction} if __name__ == '__main__': server.run(debug=True)
from config import Config from processor import Processor from classifier import Classifier from annotator import Annotator if __name__ == '__main__': # Load the config file print("Load the configuration file...") config = Config() print("done!") # Load the train data processor = Processor(config, "ner") # Load the classifier classifier = Classifier(config, processor) print("Training the model...") classifier.train() print("Begin to compute the F1-Score") classifier.test() # Load the annotator # annotator = Annotator(config, classifier) # results = annotator.write() print("complete!")
def train_and_evaluate(batch_size=8, img_dir='mess_cb', out_size=32, f_size=31, horizontal_flip=True, vertical_flip=True, width_shift_range=0.03, height_shift_range=0.03, rotation_range=360, zoom_range=0.03, exp_name='exp0.hdf5', n_blocks=4, context_blocks=1, patience=75): global X_train, Y_train, X_oracle, Y_oracle, X_val, Y_val # Initialization active_learning_mode = 1 epochs = 10 checkpointer = ModelCheckpoint(filepath=exp_name, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False) early1 = EarlyStopping(monitor='val_acc', patience=2, verbose=1) early2 = EarlyStopping(monitor='val_acc', patience=1, verbose=1) callbacks1 = [checkpointer, early1] callbacks2 = [checkpointer, early2] ##################################################################################################### # Loading saved model here if LOAD_MODEL == True: model = load_model(Expt_load) else: classifier = Classifier() model = classifier.prepare_to_init(2e-5) model.fit(X_train, Y_train, epochs=1, validation_data=(X_val, Y_val), verbose=1, callbacks=callbacks1) print 'Model trained with top layers' model = classifier.prepare_to_finetune(2e-5) model.fit(X_train, Y_train, epochs=epochs, validation_data=(X_val, Y_val), verbose=1, callbacks=callbacks2) print 'Finetuned the model' print 'Saving the model' model.save(Expt_save) while active_learning_mode: model.fit(X_train, Y_train, epochs=epochs, validation_data=(X_val, Y_val), shuffle=True, verbose=1, callbacks=callbacks2) pred_prob = model.predict(X_oracle, batch_size=400, verbose=1) train_prob = model.predict(X_train, batch_size=64, verbose=1) active_learning_mode = active_learning(pred_prob, train_prob)
from vae import MNIST_VAE from data_utils import get_data, session_num from torch import optim import torch import json dataset_used = 'MNIST' device = 'cuda' log_interval = 100 epochs = 20 batch_size = 512 vae_model = MNIST_VAE(latent_dim=128).to(device) vae_model_name = 'gr_vae_' + dataset_used + '_' + str(session_num) main_model = Classifier(n_class=10).to(device) main_model_name = 'gr_classifier_' + dataset_used + '_' + str(session_num) if session_num > 1: last_model_name = 'gr_classifier_' + dataset_used + '_' + str(session_num - 1) # main_model.load_state_dict(torch.load('models/' + last_model_name + '.pth')) last_vae_model_name = 'gr_vae_' + dataset_used + '_' + str(session_num - 1) # vae_model.load_state_dict(torch.load('models/' + last_vae_model_name + '.pth')) last_main_model = Classifier(n_class=10).to(device) last_main_model.load_state_dict( torch.load('models/' + last_model_name + '.pth')) last_main_model.eval() last_vae_model = MNIST_VAE(latent_dim=128).to(device) last_vae_model.load_state_dict( torch.load('models/' + last_vae_model_name + '.pth'))
start_dir = 1 end_dir = 540 dirs = range(start_dir, end_dir + 1) def enum(num_digits, count): numero = [0] * num_digits for i in range(num_digits): numero[i] = count % 10 count = int(count / 10) return ''.join(str(digit) for digit in reversed(numero)) p = Parser() p.parse_domain(data_path + "domain.aggl") c = Classifier(p.action_list) for i in dirs: path = data_path + enum(5, i) + "/" # One initModel.xml per dir flag = True try: p.parse_initM(path + enum(5, i) + ".xml") except: flag = False print("File not found : " + path + enum(5, i) + ".xml") if flag: for file in os.listdir(path): if file.endswith(".aggt"): if os.stat(path + file + ".plan").st_size != 0: try:
import sys import json sys.path.append('../src') from classifier import Classifier clf = Classifier() f = open('nsfc_test.json', 'r', encoding='utf-8') s = f.read() j = json.loads(s, encoding='utf-8') data = j['nsfc'] for level in [1, 2, 3]: cnt = 0 top1 = 0 top5 = 0 length = level * 2 + 1 for item in data: if len(item['sid']) < length: continue subject = clf.classify([item['title']], level=level, lang_zh=True) if subject == {}: continue cnt += 1 if subject['level{}'.format(level)][0]['code'] == item['sid'][0:length]: top1 += 1 for ret in subject['level{}'.format(level)]: if ret['code'] == item['sid'][0:length]: top5 += 1 break print('level', level, ':', top1/cnt, ' ', top5/cnt, ' ', cnt)
from bounding_box import BoundingBox # Hyperparameters detect = True test = True image_size = (1280,720) num_examples = 85 hm_threshold = 0 time_range = range(0,1260) # time_range = range(600,700,10) # Train classifier if detect: test_image = cv2.imread('test_images/test6.jpg') svm = Classifier(num_examples,test_image) svm.train() # See test images if test: for image_name in os.listdir('test_images/'): # Dont use hidden files if image_name[0] == '.': continue # Read image and convert to RGB image = cv2.imread('test_images/'+image_name) image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB) classified_image, bounding_boxes = svm.debug_classify(image)
vprint(verbose, "[+] Time budget for this task %5.2f sec" % time_budget) time_spent = time.time() - start vprint( verbose, "[+] Remaining time after reading data %5.2f sec" % (time_budget - time_spent)) if time_spent >= time_budget: vprint(verbose, "[-] Sorry, time budget exceeded, skipping this task") execution_success = False continue # ========= Creating a model, knowing its assigned task from D.info['task']. # The model can also select its hyper-parameters based on other elements of info. vprint(verbose, "======== Creating model ==========") M = Classifier() # ========= Reload trained model if it exists. vprint( verbose, "**********************************************************************" ) vprint( verbose, "****** Attempting to reload model (from res/) to avoid training ******" ) vprint( verbose, "**********************************************************************" ) you_must_train = 1
from application import app from flask import Response, request from classifier import Classifier import numpy as np import cv2 import io myClassifier = Classifier() # route http posts to this method @app.route('/predict_image', methods=['POST']) def predictImage(): global myClassifier r = request photo = r.files['photo'] in_memory_file = io.BytesIO() photo.save(in_memory_file) # image processing nparray = np.fromstring(in_memory_file.getvalue(), dtype=np.uint8) img = cv2.imdecode(nparray, cv2.IMREAD_COLOR) # classify image response = myClassifier.classify_image(image=img) return Response(response=response, status=200, mimetype="application/json") @app.route('/predict_image', methods=['GET']) def predictImage(): response = "{response: this uri accepts multipart image requests}"
# yaml config file cfg = yaml.safe_load(open("./config.yaml")) # init path collector path_coll = PathCollector(cfg) # -- # mic (for sound capture) # window and hop size N, hop = int( cfg['feature_params']['N_s'] * cfg['feature_params']['fs']), int( cfg['feature_params']['hop_s'] * cfg['feature_params']['fs']) # create classifier classifier = Classifier(path_coll=path_coll, verbose=True) # create mic instance mic = Mic(classifier=classifier, feature_params=cfg['feature_params'], mic_params=cfg['mic_params'], is_audio_record=True) # -- # game setup # init pygame pygame.init() # init display screen = pygame.display.set_mode(cfg['game']['screen_size'])
with open('data.csv') as csvfile: reader = csv.DictReader(csvfile, fieldnames=["abstract", "category"]) for row in reader: data.append({'abstract': row['abstract'], 'category': row['category']}) if row['category'] not in categories: categories.append(row['category']) random.shuffle(data) split = int(0.8 * len(data)) train_data = data[:split] test_data = data[split:] print len(test_data) print len(train_data) clf = Classifier(categories) #Training for item in train_data: clf.train(item['abstract'], item['category']) #Testing arr = [] for item in test_data: arr.append(clf.test(item['abstract'], item['category'])) #Confusion Matrix name_to_num = {} for i, category in enumerate(categories): name_to_num[category] = i
def load_classifier(sess): p = params.classifier classifier = Classifier(p.size, p.n_channels, p.n_classes) classifier.saver.restore(sess, p.model_path) return classifier