def evaluate(predicate_func, title_transformation_func, title_file_path="reuters.txt", doc_data_dir="/cs/puls/tmp/Capitalization/reuters-text", pass_doc=False): # Head word title should be different for monocase total_correct_n = 0. total_n = 0 logger.info("Evaluation of %r starts..", predicate_func) with open(title_file_path, "r", "utf8") as f: total_instance_number = 10000. finished_instance_number = 0 for l in f: if finished_instance_number == total_instance_number: break fname, raw_title = json.loads(l) raw_words = nltk.word_tokenize(raw_title) try: cap_words = title_transformation_func(title_words=raw_words) except: logger.error( "%s encountered error in making capitalized title." % (fname)) traceback.print_exc(file=sys.stdout) continue if pass_doc: with open("%s/%s" % (doc_data_dir, fname), "r", "utf8") as f: kwargs = {"doc": f.read()} else: kwargs = {} normalized_words = predicate_func(words=cap_words, **kwargs) correct_labels = [get_label(w) for w in raw_words] try: predicted_labels = [get_label(w) for w in normalized_words] except: logger.error("%s encountered error in recovery." % (fname)) traceback.print_exc(file=sys.stdout) continue total_correct_n += len( filter(lambda (rw, nw): rw == nw, zip(raw_words, normalized_words))) total_n += len(correct_labels) finished_instance_number += 1 if finished_instance_number % 1000 == 0: logger.info("%f finished", finished_instance_number / total_instance_number) print total_correct_n / total_n
def evaluate(predicate_func, title_transformation_func, title_file_path="reuters.txt", doc_data_dir="/cs/puls/tmp/Capitalization/reuters-text", pass_doc=False): # Head word title should be different for monocase total_correct_n = 0. total_n = 0 logger.info("Evaluation of %r starts..", predicate_func) with open(title_file_path, "r", "utf8") as f: total_instance_number = 10000. finished_instance_number = 0; for l in f: if finished_instance_number == total_instance_number: break fname, raw_title = json.loads(l) raw_words = nltk.word_tokenize(raw_title) try: cap_words = title_transformation_func(title_words = raw_words) except: logger.error("%s encountered error in making capitalized title." %(fname)) traceback.print_exc(file=sys.stdout) continue if pass_doc: with open("%s/%s" %(doc_data_dir, fname), "r", "utf8") as f: kwargs = {"doc": f.read()} else: kwargs = {} normalized_words = predicate_func(words = cap_words, **kwargs) correct_labels = [get_label(w) for w in raw_words] try: predicted_labels = [get_label(w) for w in normalized_words] except: logger.error("%s encountered error in recovery." %(fname)) traceback.print_exc(file=sys.stdout) continue total_correct_n += len(filter(lambda (rw, nw): rw == nw, zip(raw_words, normalized_words))) total_n += len(correct_labels) finished_instance_number += 1 if finished_instance_number % 1000 == 0: logger.info("%f finished", finished_instance_number / total_instance_number) print total_correct_n / total_n
def main(_): t1 = t.time() host, port = FLAGS.server.split(':') channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) # request = predict_pb2.PredictRequest() predict = [] image_data = img_data(image_path) imag_data = [] for i in range(len(image_data)): imag_data.append(image_data[i].numpy().tolist()) t2 = t.time() for h in range(0, 5): request = model_name(h) t3 = t.time() # Build a batch of images. request.inputs['input_1'].CopyFrom( tf.make_tensor_proto(imag_data, shape=[len(image_data), 224, 224, 3])) result_future = stub.Predict.future(request, 10.0) # 10 secs timeout result = result_future.result() t4 = t.time() classes = result.outputs['output_1'].float_val prediction = [] for i in range(0, 80): prediction.append(classes[i]) if len(prediction) % 16 == 0: classification = np.argmax(prediction) predict.append(classification) prediction = [] print(predict) fine_label = get_label(max(predict, key=predict.count)) print(fine_label, t4 - t2, t2 - t1, t4 - t1)
def cv_evaluate_model(model, dframe): X = get_featues(dframe) y = get_label(dframe) kfold = StratifiedKFold(n_splits=10, shuffle=True) accuracy = np.empty(10) precision = np.empty(10) recall = np.empty(10) for i, (train_idx, test_idx) in tqdm(enumerate(kfold.split(X, y))): model.train(dframe.iloc[train_idx]) predictions = model.predict(X.iloc[test_idx]) accuracy[i] = accuracy_score(y[test_idx], predictions) precision[i] = precision_score(y[test_idx], predictions, average='macro') recall[i] = recall_score(y[test_idx], predictions, average='macro') results = { 'params': model.get_params(), 'model': model.name, 'accuracy_std': np.std(accuracy), 'accuracy_mean': np.mean(accuracy), 'precision_std': np.std(precision), 'precision_mean': np.mean(precision), 'recall_std': np.std(recall), 'recall_mean': np.mean(recall) } return results
def prediction(): t1 = t.time() predict = [] img = img_data(image_path) # print(img[1]) print(t.time() - t1) t2 = t.time() for a in range(0, 1): SERVER_URL = url(a) for i in range(0, 5): j = json.dumps(img[i].numpy().tolist()) predict_request = '{"instances":%s}' % j # print(t.time()-t1) # print(predict_request) start_time = t.time() response = requests.post(SERVER_URL, data=predict_request) print(t.time() - start_time) # print(type(response)) # t2=t.time() prediction = response.json()['predictions'][0] # print(t.time()-t2) classification = np.argmax(prediction) # print(t.time()-t2) predict.append(classification) # print(t.time()-t2) print(predict) fine_label = get_label(max(predict, key=predict.count)) print(t.time() - t1, t.time() - t2) print(fine_label)
def callback_train(update, context): train_data = context.chat_data['train_data'] menu = ''.join(update.callback_query.data.split(', ')[1:]) store_training(train_data, get_label(menu)) context.bot.edit_message_text( f"{menu}을/를 선택하였습니다.", chat_id=update.callback_query.message.chat_id, message_id=update.callback_query.message.message_id)
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf import tensorflow.keras as keras import tensorflow.keras.layers as layers import data from matplotlib import pyplot as plt train_x = tf.convert_to_tensor( data.get_description('..\\track1_round1_train_20210222.csv')) train_y = tf.convert_to_tensor( data.get_label('..\\track1_round1_train_20210222.csv')) print(train_x.shape) test_x, test_y = data.get_test('..\\track1_round1_train_20210222.csv') rnn_units = 64 input_layer = keras.Input(shape=(104, 1)) # x = layers.Embedding(input_dim=859, output_dim=10,mask_zero='True')(input_layer) x = layers.LSTM(rnn_units, return_sequences=True, recurrent_initializer='orthogonal', activation='tanh')(input_layer) x = layers.LSTM(rnn_units, return_sequences=True, recurrent_initializer='orthogonal', activation='tanh', dropout=0.5)(x) x = layers.Attention()([x, x])
import numpy as np import time as t import config import os if __name__ == '__main__': # GPU settings # os.environ['CUDA_VISIBLE_DEVICES']='0' #get image dataset # t1=t.time() img = img_data(image_path) t1=t.time() predict=[] #load model # for i in range (0,5): model=get_model(1) t2=t.time() for j in range(len(img)): predictions=model(img[j]) classification = np.argmax(predictions) # print(classification) predict.append(classification) # print(t.time()-t2) # print(t2) print(predict) fine_label=get_label(max(predict, key=predict.count)) print(fine_label) print(t.time()-t2)
def train(self, dframe): X = get_features(dframe) y = get_label(dframe) self.clf.fit(X, y)
def createResultsForModels(trained_model1, trained_model2): succ = 0 succ_adv = 0 succ_adv_f = 0 succ_f = 0 succ_f_adv = 0 succ_f_adv_f = 0 total = 0 results = np.zeros((37,13)) for letter in letters: label_index = get_label_index(letters, letter) for filename in os.listdir('dataset3/validation/' + letter): # Load the image imToTest = loadImageFromPath('dataset3/validation/' + letter + '/' + filename) # Generate adversarial sample imToTest_adv = generate_adversarial(trained_model1, imToTest, tf.one_hot(label_index, 36), 0.2, False) imToTest_adv_features = generate_adversarial(trained_model2, imToTest, tf.one_hot(label_index, 36), 0.2, True) # Get perdictions of the original and adversarial sample for basic model label = trained_model1.predict(imToTest) label_adv = trained_model1.predict(imToTest_adv) label_adv_f = trained_model1.predict(imToTest_adv_features) # Get perdictions of the original and adversarial sample for features model label_f = trained_model2.predict(imToTest) label_f_adv = trained_model2.predict(imToTest_adv) label_f_adv_f = trained_model2.predict(imToTest_adv_features) # Get label for classification from features labels label_f = label_f[0] label_f_adv = label_f_adv[0] label_f_adv_f = label_f_adv_f[0] #Check if the predictions were correct if (get_label(letters, label)[0] == letter): succ = succ + 1 results[label_index,1] += 1 if (get_label(letters, label_adv)[0] == letter): succ_adv = succ_adv + 1 results[label_index,2] += 1 if (get_label(letters, label_adv_f)[0] == letter): succ_adv_f = succ_adv_f + 1 results[label_index,3] += 1 if (get_label(letters, label_f)[0] == letter): succ_f = succ_f + 1 results[label_index,4] += 1 if (get_label(letters, label_f_adv)[0] == letter): succ_f_adv = succ_f_adv + 1 results[label_index,5] += 1 if (get_label(letters, label_f_adv_f)[0] == letter): succ_f_adv_f = succ_f_adv_f + 1 results[label_index,6] += 1 total += 1 results[label_index,0] += 1 if total % 250 == 0 : print("Current ===== ", total) results[label_index,1:] = results[label_index,1:] / results[label_index,0] results[36,0] = total results[36,1] = succ/total results[36,2] = succ_adv/total results[36,3] = succ_adv_f/total results[36,5] = succ_f/total results[36,6] = succ_f_adv/total results[36,7] = succ_f_adv_f/total print("FINISHED") return results
def train(self, dframe): # from preprocess.py X = get_featues(dframe) y = get_label(dframe) self.clf.fit(X, y)
def plot_learning_curve(self, dframe): X = get_featues(dframe) y = get_label(dframe) return skplt.plot_learning_curve(self.clf, X, y)
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf import tensorflow.keras as keras import tensorflow.keras.layers as layers import data from matplotlib import pyplot as plt train_x = tf.convert_to_tensor(data.get_description('track1_round1_train_20210222.csv')) train_y = tf.convert_to_tensor(data.get_label('track1_round1_train_20210222.csv')) print(train_x.shape) test_x, test_y = data.get_test('track1_round1_train_20210222.csv') rnn_units = 64 input_layer = keras.Input(shape=(104,1)) # x = layers.Embedding(input_dim=859, output_dim=10,mask_zero='True')(input_layer) # x = layers.Bidirectional(layers.LSTM(rnn_units,return_sequences=False, recurrent_initializer='orthogonal',dropout=0.4))(input_layer) # x = layers.Bidirectional(layers.LSTM(rnn_units,return_sequences=False, recurrent_initializer='orthogonal'))(x) x = layers.LSTM(rnn_units,return_sequences=False, recurrent_initializer='orthogonal', activation='tanh')(input_layer) x = layers.Dense(64, activation='relu')(x) x = layers.Attention # x = layers.Embedding(input_dim=256, output_dim=256)(x) # x = layers.LSTM(rnn_units,return_sequences=True, recurrent_initializer='orthogonal', activation='tanh')(x) # x = layers.LSTM(rnn_units,return_sequences=True, recurrent_initializer='orthogonal', activation='tanh',dropout=0.4)(x) x = layers.LSTM(rnn_units,return_sequences=False, recurrent_initializer='orthogonal', activation='tanh',dropout=0.5)(x) # x = layers.Dropout(rate=0.5)(x) # x = layers.Dense(128, activation='relu')(x) x = layers.Dense(64, activation='relu')(x)