def __init__(self): self.source_lang = h.load_data('data/small_vocab_en') self.target_lang = h.load_data('data/small_vocab_fr') ((self.source_int_text, self.target_int_text), \ (self.source_vocab_to_int, self.target_vocab_to_int), \ _) = h.preprocess(self.source_lang, self.target_lang)
def telemetry(sid, data): if data: angle = float(data["steering_angle"]) throttle = float(data["throttle"]) speed = float(data["speed"]) image = Image.open(BytesIO(base64.b64decode(data["image"]))) try: image = np.asarray(image) image = preprocess(image) image = np.array([image]) angle = float(model.predict(image, batch_size=1)) global speed_limit if speed > speed_limit: speed_limit = min_speed else: speed_limit = max_speed throttle = 1.0 - ((angle**2)*1) - (speed/speed_limit)**2 throttle = throttle * 0.5 print('angle: {} throttle: {} speed: {}'.format(angle, throttle, speed)) send_control(angle, throttle) except Exception as e: print(e) else: socket.emit('manual', data={}, skip_sid=True)
def main(args): # set up and reset game environment env = gym.make('MsPacman-v0') env.reset() # initialize networks QNetwork = Network(name='QNetwork', hidden_size=args.hidden_size, learning_rate=args.learning_rate, action_size=env.action_space.n, history_size=args.history_size) target = Network(name='Target', hidden_size=args.hidden_size, learning_rate=args.learning_rate, action_size=env.action_space.n, history_size=args.history_size) saver = tf.train.Saver() # initialize history history = np.zeros((88, 80, args.history_size + 1), dtype=np.uint8) # load game with tf.Session() as sess: print("SESSION STARTED") saver.restore(sess, "model/model8160.ckpt") print("MODEL RESTORED") for game in range(args.games): # start game & initialize memory state = env.reset() for i in range(5): history[:, :, i] = preprocess(state) reward_total = 0 while True: action = np.argmax(QNetwork.predict(sess, [history[:,:,:args.history_size]])) new_state, reward, done, _ = env.step(action) # history updates history[:,:, args.history_size] = preprocess(new_state) # add new state at end history[:,:,:args.history_size] = history[:,:,1:] # shift history if done: break else: # Update reward total reward_total = reward_total + reward print(reward_total)
def infer(model, fnImg): "recognize text in image provided by file path" img = preprocess(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), Model.imgSize) batch = Batch(None, [img]) (recognized, probability) = model.inferBatch(batch, True) print('Recognized without Correction:', '"' + recognized[0] + '"') print('Recognized With Correction: ', correct_word(recognized[0])) print('Probability:', probability[0])
def getNext(self): "iterator" batchRange = range(self.currIdx, self.currIdx + self.batchSize) gtTexts = [self.samples[i].gtText for i in batchRange] imgs = [ preprocess( cv2.imread(self.samples[i].filePath, cv2.IMREAD_GRAYSCALE), self.imgSize, self.dataAugmentation) for i in batchRange ] self.currIdx += self.batchSize return Batch(gtTexts, imgs)
def find_entities(reply): tagged_reply = pos_tag(preprocess(reply)) nouns = extract_nouns(tagged_reply) tokens = word2vec(" ".join(nouns)) category = word2vec(blank) result = get_similarity(tokens, category) result.sort(key=lambda x: x[2]) if len(result) < 1: return blank else: return result[-1][0]
def experiment(n, k): """ 模拟一次实验,n为迭代次数,k为步长 """ #训练数据 D_train = generatedata(100) X_train, y_train = preprocess(D_train) #测试数据 D_test = generatedata(1000) X_test, y_test = preprocess(D_test) #训练模型 W, W_hat, w_hat, error = Pocket_PLA(X_train, y_train, k, n) #计算错误率 ein = np.mean(np.sign(W.dot(X_train.T)) != y_train, axis=1) ein_hat = np.mean(np.sign(W_hat.dot(X_train.T)) != y_train, axis=1) eout = np.mean(np.sign(W.dot(X_test.T)) != y_test, axis=1) eout_hat = np.mean(np.sign(W_hat.dot(X_test.T)) != y_test, axis=1) return ein, ein_hat, eout, eout_hat
def process(im): # Convert to RGB (to comply with helper.preprocess()) im = im[:, :, ::-1] # Preprocess im = helper.preprocess(im) # Predict prediction = model.predict(im) # Print results prediction = prediction.flatten() top_idx = np.argsort(prediction)[::-1][:5] for i, idx in enumerate(top_idx): print("{}. {:.2f} {}".format(i + 1, prediction[idx], id2label[idx]))
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = float(data["steering_angle"]) # The current throttle of the car throttle = float(data["throttle"]) # The current speed of the car speed = float(data["speed"]) # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) try: image_array = np.asarray(image) #image_array = crop(image_array, 0.35, 0.1) #image_array = resize(image_array, new_dim=(66, 200)) image_array = helper.preprocess(image_array) transformed_image_array = image_array[None, :, :, :] # This model currently assumes that the features of the model are just the images. Feel free to change this. steering_angle = float( model.predict(transformed_image_array, batch_size=1)) # The driving model currently just outputs a constant throttle. Feel free to edit this. global speed_limit if speed > speed_limit: speed_limit = MIN_SPEED # slow down else: speed_limit = MAX_SPEED throttle = 1.0 - steering_angle**2 - (speed / speed_limit)**2 print('{:.5f}, {:.1f}'.format(steering_angle, throttle)) send_control(steering_angle, throttle) except Exception as e: print(e) else: sio.emit('manual', data={}, skip_sid=True)
def telemetry(sid, data): if data: # current Steering angle of vehicle steering_angle = float(data['steering_angle']) # current throttle of vehicle throttle = float(data["throttle"]) # currentt speed of vehicle speed = float(data["speed"]) # current image from center camera of vehicle image = Image.open(BytesIO(base64.b64decode(data['image']))) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) try: image = np.asarray(image) # from PIL image to numpy array image = helper.preprocess(image) # apply preprocessing image = np.array([image]) # the model expects 4D array # predict the steering angle for the image steering_angle = float(model.predict(image, batch_size=1)) # lower the throttle as the speed increases # if the speed is above the current speed limit, we are on a downhill. # make sure we slow down first and then go back to the original max speed. global speed_limit if speed > speed_limit: speed_limit = MIN_SPEED # slow vehicle down else: speed_limit = MAX_SPEED throttle = 1.0 - steering_angle**2 - (speed / speed_limit)**2 print('{} {} {}'.format(steering_angle, throttle, speed)) send_control(steering_angle, throttle) except Exception as e: print(e) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def prediciton(input_file, output_file, model_file='./models/model', dict_file='./dictionaries/dictionary'): ''' Generates prediction written to an output file in the following format: {"_id": "abcdef", "topics": ["topic1", "topic2", "topic3", "topic4", "topic5”]} ''' #load model lda_model = gensim.models.LdaMulticore.load(model_file) #load dictionary dictionary = gensim.corpora.Dictionary.load(dict_file) dict_ = import_json(input_file) _id = dict_['_id'] text = dict_['text'] bow_vector = dictionary.doc2bow(preprocess(text)) # Save top 5 topics output_data = {} topics = [] topic_length = 5 count = 0 for index, score in sorted(lda_model[bow_vector], key=lambda tup: -1 * tup[1]): if (count == 5): break topics.append(lda_model.print_topic(index, topic_length)) count += 1 output_data["_id"] = _id output_data["topics"] = topics # write to output file with open(output_file, 'w') as data_file: json.dump(output_data, data_file)
def main(): # Read in data os.chdir( "/Users/MichaelChoie/Desktop/Data Science/deep-learning/embeddings") text = helper.read_data() # Process data words = helper.preprocess(text) vocab_to_int, int_to_vocab = helper.create_lookup_table(words) int_words = [vocab_to_int[word] for word in words] train_words = helper.subsampling(int_words) # Create directory for model checkpoints if not os.path.exists("checkpoints"): os.mkdirs("checkpoints") # Build computational graph embed_mat = build_graph(train_graph, int_to_vocab, train_words) # Visualize network visualize(embed_mat)
def train(): ''' Generate the trained LDA model and a dictionary object ''' # Preprocess all text samples processed_docs = [preprocess(i['text']) for i in list_dict] # Bag of words on the dataset dictionary = gensim.corpora.Dictionary(processed_docs) # filter extremes dictionary.filter_extremes(no_below=15, no_above=0.5, keep_n=100000) #For each document we create a dictionary reporting how many #words and how many times those words appear. Save this to ‘bow_corpus’, then check our selected document earlier. bow_corpus = [dictionary.doc2bow(doc) for doc in processed_docs] bow_doc_0 = bow_corpus[0] for i in range(len(bow_doc_0)): print("Word {} (\"{}\") appears {} time.".format( bow_doc_0[i][0], dictionary[bow_doc_0[i][0]], bow_doc_0[i][1])) # Running LDA using Bag of Words lda_model = gensim.models.LdaMulticore(bow_corpus, num_topics=10, id2word=dictionary, passes=2, workers=2) for idx, topic in lda_model.print_topics(-1): print('Topic: {} \nWords: {}'.format(idx, topic)) return dictionary, lda_model
def main(): # read data (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() # read data (x_train, y_train, x_test, y_test) = preprocess(x_train, y_train, x_test, y_test) # Reshape the data inputs such that we can put those inputs into MLP train_inputs = np.reshape(x_train, (-1, 28 * 28)) test_inputs = np.reshape(x_test, (-1, 28 * 28)) orig_model = MLP(28 * 28) # train original model epochs = 10 print("Training Original MLP...") for i in range(epochs): train(orig_model, train_inputs, y_train) test_acc = test(orig_model, test_inputs, y_test) print("Epoch: {} ------ Testing accuracy: {}".format(i + 1, test_acc)) # calculate fgs and deepfool for original model, on both test set and training set print("Creating DeepFool images set... will take aobut 5 mins") (train_adv_orig, train_r_orig) = deepfool(orig_model, train_inputs) (test_adv_orig, test_r_orig) = deepfool(orig_model, test_inputs) # fine tuning tuning_model = MLP_tuning(28 * 28, orig_model) epochs = 5 print("Training Fine Tuning MLP...") for i in range(epochs): train(tuning_model, train_adv_orig, y_train) tuning_test_acc = test(tuning_model, test_adv_orig, y_test) print("Epoch: {} ------ Testing accuracy: {}".format( i + 1, tuning_test_acc)) # train deepdefense model regu_model = regu_MLP(28 * 28, orig_model) epochs = 5 print("Training Deep Defense MLP...") for i in range(epochs): regu_train(regu_model, train_adv_orig, y_train, train_r_orig) regu_test_acc = test(regu_model, test_adv_orig, y_test) print("Epoch: {} ------ Testing accuracy: {}".format( i + 1, regu_test_acc)) # keep training original model for comparison epochs = 5 print("Training MLP for 5 more epochs...") for i in range(epochs): train(orig_model, train_inputs, y_train) test_accu = test(orig_model, test_inputs, y_test) print("Epoch: {} ------ Testing accuracy: {}".format(i + 1, test_accu)) ################### Evaluation ######################### # ROC curve on deepfool testing image generated from origianl MLP model roc1 = roc(orig_model, test_adv_orig, y_test, "Vanilla MLP") roc2 = roc(tuning_model, test_adv_orig, y_test, "Fine tuning MLP") roc3 = roc(regu_model, test_adv_orig, y_test, "Deep Defense MLP") AUC = pd.DataFrame( { "Vanilla MLP": list(roc1.values()), "Fine-Tune MLP": list(roc2.values()), "Deep Defense": list(roc3.values()) }, index=["label " + str(i + 1) for i in range(10)]) print("Area Under the Curve:") print(AUC) # testing acc on benign images benign_test_acc = pd.DataFrame( { "Vanilla MLP": test(orig_model, test_inputs, y_test), "Fine-Tune MLP": test(tuning_model, test_inputs, y_test), "Deep Defense": test(regu_model, test_inputs, y_test) }, index=["TestAcc"]) # rho2 scores (test_adv_orig2, test_r_orig2) = deepfool(orig_model, test_inputs) (test_adv_tuning, test_r_tuning) = deepfool(tuning_model, test_inputs) (test_adv_regu, test_r_regu) = deepfool(regu_model, test_inputs) regu_rho2 = rho2(test_r_regu, test_inputs) tuning_rho2 = rho2(test_r_tuning, test_inputs) orig_rho2 = rho2(test_r_orig2, test_inputs) rho2_all = pd.DataFrame( { "Vanilla MLP": orig_rho2, "Fine-Tune MLP": tuning_rho2, "Deep Defense": regu_rho2 }, index=["Rho2 Score"]) # plot accuracy on FGS images epsilon_ref_100, epsilon_ref_50, epsilon_ref_20 = plot_acc_on_FGS( orig_model, regu_model, tuning_model, test_inputs, y_test, test_adv_orig) epsilon_list = [epsilon_ref_20, epsilon_ref_50, epsilon_ref_100] # calculating testing accuracy of vanilla, regu, and finetune on FGS examples with these three epsilon values pert_test_orig = FGS(orig_model, test_inputs, y_test, 1) pert_test_regu = FGS(regu_model, test_inputs, y_test, 1, True, test_adv_orig) pert_test_tuning = FGS(tuning_model, test_inputs, y_test, 1) FGS_orig_test_acc = list( map( lambda x: test(orig_model, x * pert_test_orig + test_inputs, y_test ), epsilon_list)) FGS_regu_test_acc = list( map( lambda x: test(regu_model, x * pert_test_regu + test_inputs, y_test ), epsilon_list)) FGS_tuning_test_acc = list( map( lambda x: test(tuning_model, x * pert_test_tuning + test_inputs, y_test), epsilon_list)) acc_fgs = pd.DataFrame( { "Vanilla MLP": FGS_orig_test_acc, "Fine-Tune MLP": FGS_tuning_test_acc, "Deep Defense": FGS_regu_test_acc }, index=["[email protected]", "[email protected]", "[email protected]"]) result_table = pd.concat([benign_test_acc, rho2_all, acc_fgs], ignore_index=False).transpose() print(result_table)
def predict_vietnamese(model, word): ''' predict and return text recognized ''' img = preprocess(word, ModelVietnamse.imgSize) minibatch = MiniBatch(None, [img]) recognized = model.inferBatch(minibatch) return recognized[0]
approximator_model.load_weights(latest) # ===== INITIALISATION ====== frame_cnt = 0 prev_lives = 5 acc_nonzeros = [] acc_actions = [] is_done = False env.reset() for episode in range(15): start_time = time.time() print(f"Running episode {episode}") initial_observation = env.reset() state = np.repeat(preprocess(initial_observation), time_channels_size + 1).reshape(state_shape) is_done = False # next_state = initial_state.copy() # To remove all the information of the last episode episode_rewards = [] frame_cnt = 0 while not is_done: # https://danieltakeshi.github.io/2016/11/25/frame-skipping-and-preprocessing-for-deep-q-networks-on-atari-2600-games/ init_mask = tf.ones([1, action_space]) init_state = state[:, :, :-1] q_values = approximator_model.predict( [tf.reshape(init_state, [1] + input_shape), init_mask]) action = np.argmax(q_values) # action = env.action_space.sample()
#Parsing the arguments ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required=True, help="path to input dataset") ap.add_argument("-m", "--model", required=True, help="path to output model") args = vars(ap.parse_args()) #Initializing the data and labels data = [] labels = [] #loop over the input images for imagePath in paths.list_images(args["dataset"]): #load the image, preprocess it and store it in the data list image = cv2.imread(imagePath) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = preprocess(image, 28, 28) image = img_to_array(image) data.append(image) #extract the label from image path and update the labels list label = imagePath.split(os.path.sep)[-2] labels.append(label) #Scaling raw pixel intensities to range[0,1] data = np.array(data, dtype=float) / 255.0 labels = np.array(labels) #Partitioning the data into training and testing splits using 80% of data #for training and the remaining 20% for testing (trainX, testX, trainY, testY) = train_test_split(data,
def predict(self, data): """ The input data has the following schema: { "instances": [ { "file": "bytes" "tags": { "StudyInstanceUID": "str", "SeriesInstanceUID": "str", "SOPInstanceUID": "str", ... } }, ... ], "args": { "arg1": "str", "arg2": "str", ... } } Model scope specifies whether an entire study, series, or instance is given to the model. If the model scope is 'INSTANCE', then `instances` will be a single instance (list length of 1). If the model scope is 'SERIES', then `instances` will be a list of all instances in a series. If the model scope is 'STUDY', then `instances` will be a list of all instances in a study. The additional `args` dict supply values that may be used in a given run. For a single instance dict, `files` is the raw binary data representing a DICOM file, and can be loaded using: `ds = pydicom.dcmread(BytesIO(instance["file"]))`. The results returned by this function should have the following schema: [ { "type": "str", // 'NONE', 'ANNOTATION', 'IMAGE', 'DICOM', 'TEXT' "study_uid": "str", "series_uid": "str", "instance_uid": "str", "frame_number": "int", "class_index": "int", "data": {}, "probability": "float", "explanations": [ { "name": "str", "description": "str", "content": "bytes", "content_type": "str", }, ... ], }, ... ] The DICOM UIDs must be supplied based on the scope of the label attached to `class_index`. """ input_instances = data["instances"] input_args = data["args"] x_arrays = [] for instance in input_instances: tags = instance["tags"] ds = pydicom.dcmread(BytesIO(instance["file"])) x_orig = ds x_arrays.append(x_orig) input_slice_number = int( input_args.get("input_slice_number", DEFAULT_INPUT_SLICE_NUMBER)) # Handles inputs with small slices if len(x_arrays) < input_slice_number: input_slice_number = len(x_arrays) x, x_un_normalized = preprocess(x_arrays, input_slice_number) self.model.eval() best_window = 0 probability = 0.0 with torch.no_grad(): for i, window in enumerate(x): cls_logits = self.model.forward( window.to(self.device, dtype=torch.float)) cls_probs = torch.sigmoid(cls_logits).to("cpu").numpy() if cls_probs[0][0] > probability: probability = cls_probs[0][0] best_window = i if not probability >= float( input_args.get("probability_threshold", DEFAULT_PROBABILITY_THRESHOLD)): result = { "type": "NONE", "study_uid": tags["StudyInstanceUID"], "series_uid": tags["SeriesInstanceUID"], "instance_uid": tags["SOPInstanceUID"], "frame_number": None, } else: result = { "type": "ANNOTATION", "study_uid": tags["StudyInstanceUID"], "series_uid": tags["SeriesInstanceUID"], "frame_number": None, "class_index": 0, "data": None, "probability": float(probability), } if input_args.get("gradcam", GRADCAM_OFF) == GRADCAM_MAX_PROBABILITY: self.grad_cam.register_hooks() with torch.set_grad_enabled(True): probs, idx = self.grad_cam.forward(x[best_window]) self.grad_cam.backward(idx=idx[0]) cam = self.grad_cam.get_cam("module.encoders.3") self.grad_cam.remove_hooks() gradcam_output_buffer = compute_gradcam_gif( cam, x[best_window], x_un_normalized[best_window]) gradcam_explanation = [{ "name": "Grad-CAM", "description": "Visualize how parts of the image affects neural network’s output by looking into the activation maps. From _Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization_ (https://arxiv.org/abs/1610.02391)", "content": gradcam_output_buffer.getvalue(), "content_type": "image/gif", }] result["explanations"] = gradcam_explanation return [result]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:4] #Sorting the contours from left to right #(in the order in which they appear in the captchaimage) cnts = contours.sort_contours(cnts)[0] #initialize the output image along with output predictions output = cv2.merge([gray] * 3) predictions = [] #loop over the contours for c in cnts: #computing the bounding box and then extracting the digit (x, y, w, h) = cv2.boundingRect(c) roi = gray[y - 5:y + h + 5, x - 5:x + w + 5] #preprocess the roi and then classify it roi = preprocess(roi, 28, 28) roi = np.expand_dims(img_to_array(roi), axis=0) / 255.0 pred = model.predict(roi).argmax(axis=1)[0] predictions.append(str(pred)) #draw the prediction on the output image cv2.rectangle(output, (x - 2, y - 2), (x + w + 4, y + h + 4), (0, 0, 255), 1) cv2.putText(output, str(pred), (x - 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.60, (255, 0, 0), 2) predictions = "".join(predictions) #show the output image print(f"Shhh Not a Robot : {predictions}") cv2.imshow("Output", output) cv2.waitKey()
def predict(path, model_): x, _ = preprocess(path, 1) x = tf.reshape(x, (-1, 227, 227, 3)) return model_(x)
from statistics import mean from sklearn.tree import export_graphviz import pydot import matplotlib.pyplot as plt import os os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/' header = ['course', 'school', 'sex', 'age','address', 'famsize', 'Pstatus', 'Medu', 'Fedu', 'Mjob', 'Fjob', 'reason', 'guardian', 'traveltime', 'studytime', 'failures', 'schoolsup', 'famsup', 'paid', 'activities', 'higher', 'internet', 'romantic', 'famrel', 'freetime', 'goout', 'Dalc', 'Walc', 'health', 'absences', 'G1', 'G2', 'G3'] feature_list = ['course', 'school', 'sex', 'age','address', 'famsize', 'Pstatus', 'Medu', 'Fedu', 'Mjob', 'Fjob', 'reason', 'guardian', 'traveltime', 'studytime', 'failures', 'schoolsup', 'famsup', 'paid', 'activities', 'higher', 'internet', 'romantic', 'famrel', 'freetime', 'goout', 'Dalc', 'Walc', 'health', 'absences', 'G1', 'G2'] df = helper.adding_header('data.csv',header) df = helper.handle_non_numerical_data(df) df = helper.preprocess(df) y= np.array(df['G3']) df = df.drop(columns =['G3'],axis =1) X= np.array(df) X_train, X_test, y_train, y_test = cross_validation.train_test_split(X,y,test_size = 0.2, random_state = 42) baseline_pred = np.mean(y) baseline_errors = abs(baseline_pred - y) print('Average baseline error: ', round(np.mean(baseline_errors), 2)) #X_train = preprocessing.scale(X_train) rf = RandomForestRegressor(n_estimators = 1000, random_state = 36)
parser = argparse.ArgumentParser() parser.add_argument('data_dir', action = 'store', help='image directory') parser.add_argument('--save_dir', action = 'store', help = 'save checkpoint') parser.add_argument('--in_file', type = str, default = "label_map.json", help = 'input json file') parser.add_argument('--arch', action = 'store', default = 'vgg19', help = 'architecture') parser.add_argument('--epochs', action = 'store', type = int, default = 6, help = 'number of epochs') parser.add_argument('--learning_rate', action = 'store', type = float, default = 0.03, help = 'learning rate') parser.add_argument('--hidden_units', action = 'store', type = int, default = 2900, help = 'number of hidden units') parser.add_argument('--out_size', action = 'store', type = int, default = 102, help = 'number of outputs') parser.add_argument('--drop_p', type = float, default = 0.5, help = 'probability of dropping the weights') parser.add_argument('--gpu', action = 'store_true', help = 'use gpu') args = parser.parse_args() json_path = args.in_file label_map = helper.load_label_map(json_path) data_dir = args.data_dir train_data, validation_data, test_data, trainloader, validloader, testloader = helper.preprocess(data_dir) model_ = classifier.build_model(args.hidden_units, len(label_map), args.drop_p, args.arch) model = helper.premodel(args.arch) for param in model.parameters(): param.requires_grad = False in_size = helper.get_size(model, args.arch) model.classifier = helper.Network(in_size, args.out_size, [args.hidden_units], drop_p = 0.5) criterion = nn.NLLLoss() optimizer = optim.SGD(model.classifier.parameters(), lr = args.learning_rate) helper.train(model, trainloader, validloader, criterion, optimizer, args.epochs, 40, args.gpu) test_accuracy, test_loss = helper.valid_loss_acc(model, testloader, criterion, args.gpu) print("Test Accuracy: {:.4f} ".format(test_accuracy), "Test Loss: {:.4f}".format(test_loss)) helper.save_checkpoint(model, train_data, optimizer, args.save_dir, args.arch)
outputs=resnet_model.get_layer(feature_layer).output) # For each data subset for datadir in DATA_SUBSETS: features = [] labels = [] paths = [] images_list = glob.glob(datadir + "/*/*.jpg") # Process images for i, path in enumerate(images_list): try: # Load image im = skimage.io.imread(path) im = helper.preprocess(im) if im is None: raise Exception("Could not load image") # Run model to get features code = features_model.predict(im).flatten() # Cache result label = NAMES_TO_IDS[os.path.basename(os.path.dirname(path))] labels.append(label) features.append(code) rel_path = os.path.join(*path.split(os.sep)[-2:]).replace( "\\", "/") paths.append(rel_path) # Show progress
image = skimage.transform.resize(image, (224, 224)) size = 75 imgs = saliency_queue(image, size, scale=1) a = skimage.filters.gaussian(im[0, :, :, :], sigma=10, multichannel=True) for i, j in enumerate(imgs[:9]): #a = skimage.filters.gaussian(im, sigma=10, multichannel=True) #a = np.zeros((1,224,224,3)) a[j[1][0]:j[1][0] + size, j[1][1]:j[1][1] + size, :] = im[0, j[1][0]:j[1][0] + size, j[1][1]:j[1][1] + size, :] # plt.figure() # plt.imshow(a[0,:,:,:]) # plt.show() a = helper.preprocess(a) looks[i, :] = features_model.predict(a).flatten() # Cache result label = NAMES_TO_IDS[os.path.basename(os.path.dirname(path))] labels.append(label) features.append(looks) rel_path = os.path.join(*path.split(os.sep)[-2:]).replace( "\\", "/") paths.append(rel_path) # Show progress if index % 100 == 0: print(index, "/", len(images_list)) except Exception as e:
""" Created on Wed Feb 13 01:14:27 2019 @author: qinzhen """ import matplotlib.pyplot as plt import numpy as np import helper as hlp plt.rcParams['font.sans-serif'] = ['SimHei'] #用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号 data_train = np.genfromtxt("hw1_18_train.txt") data_test = np.genfromtxt("hw1_18_test.txt") X_train, y_train = hlp.preprocess(data_train) X_test, y_test = hlp.preprocess(data_test) #problem 18 hlp.f2(hlp.Pocket_PLA, X_train, y_train, X_test, y_test, 2000, max_step=50) #problem 19 result = hlp.f2(hlp.PLA, X_train, y_train, X_test, y_test, 2000, 1, max_step=50)
'failures', 'schoolsup', 'famsup', 'paid', 'activities', 'higher', 'internet', 'romantic', 'famrel', 'freetime', 'goout', 'Dalc', 'Walc', 'health', 'absences', 'G1', 'G2', 'G3' ] feature_list = [ 'course', 'school', 'sex', 'age', 'address', 'famsize', 'Pstatus', 'Medu', 'Fedu', 'Mjob', 'Fjob', 'reason', 'guardian', 'traveltime', 'studytime', 'failures', 'schoolsup', 'famsup', 'paid', 'activities', 'higher', 'internet', 'romantic', 'famrel', 'freetime', 'goout', 'Dalc', 'Walc', 'health', 'absences', 'G1', 'G2' ] df = helper.adding_header('data.csv', header) df = helper.handle_non_numerical_data(df) df = helper.preprocess(df) y = np.array(df['G3']) df = df.drop(columns=['G3'], axis=1) X = np.array(df) X_train, X_test, y_train, y_test = cross_validation.train_test_split( X, y, test_size=0.2, random_state=42) baseline_pred = np.mean(y) baseline_errors = abs(baseline_pred - y) print('Average baseline error: ', round(np.mean(baseline_errors), 2)) #X_train = preprocessing.scale(X_train) rf = RandomForestRegressor(n_estimators=1000, random_state=36) rf.fit(X_train, y_train)
def main(args): # create directory for logs if not os.path.exists(os.path.join(args.log_dir, args.run_num)): logging.info("Creating directory {0}".format( os.path.join(args.log_dir, args.run_num))) os.mkdir(os.path.join(args.log_dir, args.run_num)) # set up and reset game environment env = gym.make('MsPacman-v0') env.reset() # reset and initialize networks (Q & Target) tf.reset_default_graph() QNetwork = Network(name='QNetwork', hidden_size=args.hidden_size, learning_rate=args.learning_rate, action_size=env.action_space.n, history_size=args.history_size) target = Network(name='Target', hidden_size=args.hidden_size, learning_rate=args.learning_rate, action_size=env.action_space.n, history_size=args.history_size) # model saver saver = tf.train.Saver() # initialize buffer & history buffer = deque(maxlen=args.buffer_size) history = np.zeros((88, 80, args.history_size + 1), dtype=np.uint8) # exploit/explore schedule epsilons = np.linspace(args.epsilon_start, args.epsilon_end, args.epsilon_explore) epsilons = list(epsilons) + list(np.repeat(args.epsilon_end, 1e7)) # Train the DQN with tf.Session() as sess: writer = tf.summary.FileWriter( os.path.join(args.log_dir, args.run_num), sess.graph) # restore checkpoint # saver.restore(sess, "model1/model880.ckpt") # start new model sess.run(tf.global_variables_initializer()) # Set up count for network reset count = 0 # Set up history for episode state = env.reset() for i in range(5): history[:, :, i] = preprocess(state) # Fill The Buffer for i in range(args.buffer_size // 20): action, _ = epsilon_greedy(sess, QNetwork, history[:, :, :args.history_size], epsilons[count]) new_state, reward, done, _ = env.step(action) # history updates history[:, :, args.history_size] = preprocess( new_state) # add new state at end history[:, :, :args.history_size] = history[:, :, 1:] # shift history new_state = np.copy(history[:, :, 1:]) # with new state old_state = np.copy( history[:, :, :args.history_size]) # without new state # Add step to buffer buffer.append([old_state, action, new_state, reward, done]) # If done, reset history if done: state = env.reset() for i in range(args.history_size + 1): history[:, :, i] = preprocess(state) break # Train for epoch in range(args.epochs): # For Tensorboard result = [] reward_total = 0 # Set Up Memory state = env.reset() # get first state for i in range(args.history_size + 1): history[:, :, i] = preprocess(state) while True: # Add M to buffer (following policy) action, max_Q = epsilon_greedy( sess, QNetwork, history[:, :, :args.history_size], epsilons[count]) new_state, reward, done, _ = env.step(action) # deal with history and state history[:, :, args.history_size] = preprocess( new_state) # add new state at end history[:, :, :args. history_size] = history[:, :, 1:] # shift history new_state = np.copy(history[:, :, 1:]) # with new state old_state = np.copy( history[:, :, :args.history_size]) # without new state # Add step to buffer buffer.append([old_state, action, new_state, reward, done]) # Add max Q to result result.append(max_Q) if count % args.update_every == 0: ### Sample & Update sample = random.sample(buffer, args.batch_size) state_b, action_b, new_state_b, reward_b, done_b = map( np.array, zip(*sample)) # Q-Network T_preds = [] TPreds_batch = target.predict(sess, new_state_b) for i in range(args.batch_size): terminal = done_b[i] if terminal: T_preds.append(reward_b[i]) else: T_preds.append(reward_b[i] + args.discount_rate * np.max(TPreds_batch[i])) # Update Q-Network loss, _ = QNetwork.update(sess, state_b, action_b, T_preds, count) # If simulation done, stop if done: # Reset history state = env.reset() for i in range(args.history_size + 1): history[:, :, i] = preprocess(state) # Tensorboard avg_max_Q = np.mean(result) loss, summary = QNetwork.display(sess, state_b, action_b, T_preds, avg_max_Q, reward_total) # Log and save models logger.info("Epoch: {0}\tAvg Reward: {1}".format( epoch, avg_max_Q)) writer.add_summary(summary, epoch) break else: reward_total = reward_total + reward # Save target network parameters every epoch count += 1 if count % args.reset_every == 0: copy_parameters(sess, QNetwork, target) # save model if epoch % 20 == 0: saver.save(sess, "./model/model{0}.ckpt".format(epoch))
import cv2 import numpy as np from PIL import Image np.set_printoptions(threshold=np.nan) from helper import preprocess, centeredCrop image = "Blender/test_data/data_24.png" X = cv2.imread(image) print(X.shape) X = cv2.resize(X, (455, 256)) X = centeredCrop(X, 224) img = Image.fromarray(X, 'RGB') img.save('my.png') img.show() new_x = preprocess([image]) print(new_x[0][0].shape) img2 = Image.fromarray(new_x[0][0], 'RGB') img2.save('my2.png') img2.show()
classifier_model.load_weights(WEIGHTS_CLASSIFIER) # Load test images paths = glob.glob(os.path.expanduser("ml/data/indoor/test/*/*.jpg")) random.shuffle(paths) # Classify images for path in paths: print( "----------------------------------------------------------------------------------------" ) print("Classifying image: ", path) # Load and preprocess image im = skimage.io.imread(path) transformed = helper.preprocess(im) if transformed is None: continue # Classify code = features_model.predict(transformed).reshape(1, -1) prediction = classifier_model.predict(code) # Print result prediction = prediction.flatten() top_idx = np.argsort(prediction)[::-1][:5] for i, idx in enumerate(top_idx): print("{}. {:.2f} {}".format(i + 1, prediction[idx], IDS_TO_NAMES[str(idx)])) # Show image
def main(): mode = args.mode source_file = args.source_file target_file = args.target_file eval_source_file = args.eval_source_file eval_target_file = args.eval_target_file save_path = args.save_path usr_sentence = args.sentence load_path = args.load_path plot_attn = args.plot_attn with open(args.config_file, 'r') as f: config = json.load(f) pprint.pprint(config) assert torch.cuda.is_available() if mode == 'train': if config['target_lang'] == 'de': source_data, source_word2idx, source_idx2word, target_data, target_word2idx, target_idx2word = helper.load_europarl_data( source_file, target_file, max_sentence_len=config['max_sentence_len'], train_frac=config['train_frac'], max_vocab_size=config['vocab_size']) # source_data, source_word2idx, source_idx2word, target_data, target_word2idx, target_idx2word = helper.load_cc_data(source_file, target_file, max_sentence_len=config['max_sentence_len'], train_frac=config['train_frac'], max_vocab_size=config['vocab_size']) elif config['target_lang'] == 'hi': source_data, source_word2idx, source_idx2word, target_data, target_word2idx, target_idx2word = helper.load_hind_en_data( source_file, train_frac=config['train_frac'], max_sentence_len=config['max_sentence_len'], max_vocab_size=config['vocab_size']) print(len(source_data)) if not os.path.exists(save_path): os.makedirs(save_path) helper.save_objects((source_word2idx, target_word2idx), os.path.join(save_path, 'word2idx.pkl')) helper.save_objects((source_idx2word, target_idx2word), os.path.join(save_path, 'idx2word.pkl')) model = build_model(config, len(source_word2idx), len(target_word2idx)) pad_idx = helper.get_tags()['<PAD>'] num_valid = int(0.2 * len(source_data)) val_src, val_tgt = helper.load_eval_data( eval_source_file, eval_target_file, source_word2idx, target_word2idx, num_valid, max_sentence_len=config['max_sentence_len'], tgt_lang=config['target_lang']) assert (len(val_src) == len(val_tgt)) train(model, source_data, target_data, len(target_word2idx), val_src, val_tgt, pad_idx, save_path, config['batch_size'], config['epochs'], config['learning_rate']) elif mode == 'translate': sentence = ' '.join(usr_sentence) src_w2i, _ = helper.load_objects(load_path, 'word2idx.pkl') src_i2w, tgt_i2w = helper.load_objects(load_path, 'idx2word.pkl') model = build_model(config, len(src_w2i), len(tgt_i2w)) model.load_state_dict(torch.load(os.path.join(load_path, 'model.pt'))) src_sent = helper.preprocess(sentence) src_length = [len(src_sent)] src_sent = [src_sent] # print(len(src_sent)) src_sent = helper.text_to_ids(src_sent, src_w2i) # print(src_length) input_sent = torch.tensor(src_sent, device=device) length = torch.tensor(src_length, device=device) translated_sents, attn_weights = translate( model, input_sent, length, tgt_i2w, max_len=config['max_sentence_len']) trans_sent = ' '.join(translated_sents[0]) print(trans_sent) if plot_attn: src_sent = [[src_i2w[word] for word in sent] for sent in src_sent] #print(attn_weights[0].shape) helper.plot_attention(attn_weights[0], src_sent[0], translated_sents[0]) elif mode == 'calc_bleu': src_w2i, tgt_w2i = helper.load_objects(load_path, 'word2idx.pkl') src_i2w, tgt_i2w = helper.load_objects(load_path, 'idx2word.pkl') model = build_model(config, len(src_w2i), len(tgt_w2i)) model.load_state_dict(torch.load(os.path.join(load_path, 'model.pt'))) src, tgt = helper.load_eval_data( eval_source_file, eval_target_file, src_w2i, tgt_w2i, 6005, max_sentence_len=config['max_sentence_len'], tgt_lang=config['target_lang'], random_shuffle=False) bleu_score = calc_bleu(model, src, tgt, src_i2w, tgt_i2w, max_len=config['max_sentence_len']) print('Bleu score: {:.2f}'.format(bleu_score)) else: parser.error('Not a valid mode try with train / translate / calc_bleu')
def find_match(reply, possible_responses): bow_user = Counter(preprocess(reply)) bow_responses = [Counter(preprocess(response)) for response in possible_responses] similarity_list = [compare(bow_user, response) for response in bow_responses] idx = similarity_list.index(max(similarity_list)) return possible_responses[idx]