def test_load_save_inputs(tmpdir): i1 = input_variable((1,2), name='i1') i2 = input_variable((2,1), name='i2') root_node = plus(i1, i2) input1 = [[[1,2]]] input2 = [[[[1],[2]]]] result = root_node.eval({i1: input1, i2: input2}) expected = [[[[2,3],[3,4]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'i_plus_i_0.mod') root_node.save_model(filename) loaded_node = load_model(filename) # Test specifying the input nodes by name loaded_result = loaded_node.eval({'i1': input1, 'i2': input2}) assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval({'i1': input1, 'i2': input2}) assert np.allclose(loaded_result, expected)
def test_load_save_inputs(tmpdir): i1 = input_variable((1, 2), name='i1') i2 = input_variable((2, 1), name='i2') root_node = plus(i1, i2) input1 = [[[1, 2]]] input2 = [[[[1], [2]]]] result = root_node.eval({i1: input1, i2: input2}) expected = [[[[2, 3], [3, 4]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'i_plus_i_0.mod') root_node.save_model(filename) loaded_node = load_model(filename) # Test specifying the input nodes by name loaded_result = loaded_node.eval({'i1': input1, 'i2': input2}) assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval({'i1': input1, 'i2': input2}) assert np.allclose(loaded_result, expected)
def init(): # download popular data, models nltk.download('popular') from cntk.ops.functions import load_model # load the model and dictionary file share_path = os.environ[ 'AZUREML_NATIVE_SHARE_DIRECTORY'] if 'AZUREML_NATIVE_SHARE_DIRECTORY' in os.environ else 'outputs/' global model model = load_model(os.path.join(share_path, 'model.cmf')) global dictionary with open('dictionary.txt', 'r', encoding='utf-8') as f: dictionary = f.read().rstrip('\r\n').split('\n') global vocab_size vocab_size = len(dictionary) global sent_normalizer sent_normalizer = SentenceNormalizer(dictionary=dictionary) global labels with open('labels.txt', 'r') as f: labels = f.read().strip().split('\n')
def load_and_sample(model_filename, vocab_filename, prime_text='', use_hardmax=False, length=1000, temperature=1.0): # load the model model = load_model(model_filename) # load the vocab chars = [c[0] for c in open(vocab_filename).readlines()] char_to_ix = {ch: i for i, ch in enumerate(chars)} ix_to_char = {i: ch for i, ch in enumerate(chars)} output = sample(model, ix_to_char, len(chars), char_to_ix, prime_text=prime_text, use_hardmax=use_hardmax, length=length, temperature=temperature) ff = open('output.txt', 'w', encoding='utf-8') ff.write(output) ff.close()
def load_and_sample(model_filename, vocab_filename, prime_text='', use_hardmax=False, length=1000, temperature=1.0): # load the model model = load_model(model_filename) # load the vocab vocab_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), vocab_filename) chars = [c[0] for c in open(vocab_filepath).readlines()] char_to_ix = {ch: i for i, ch in enumerate(chars)} ix_to_char = {i: ch for i, ch in enumerate(chars)} return sample(model, ix_to_char, len(chars), char_to_ix, prime_text=prime_text, use_hardmax=use_hardmax, length=length, temperature=temperature)
def predictView(request): model = load_model(os.path.join(settings.BASE_DIR, "data/myModel.model")) post_data = request.POST.items() pd = [p for p in post_data] imgData1 = pd[1][0].replace(" ", "+") imgData1 += "=" * ((4 - len(imgData1) % 4) % 4) filename = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(32) ]) convertImage(imgData1, filename) image = Image.open(filename + '.png').convert('1') image.thumbnail((28, 28), Image.ANTIALIAS) image_np = np.array(image.getdata()).astype(int) image_np_expanded = np.expand_dims(image_np, axis=0) predicted_label_probs = model.eval({model.arguments[0]: image_np_expanded}) data = np.argmax(predicted_label_probs, axis=1) return JsonResponse({"data": str(data[0])})
def init_server(): global labels global model global features_by_range global approval_ratings global features_by_day global articles_for_day print('Initializing server') approval_ratings, political_article_corpora = Runner.init_corpora() features_by_day = Runner.corpora_to_day_features(political_article_corpora) features_by_range = Runner.combine_day_ranges(features_by_day, approval_ratings) print('Loading model from disk: ' + Config.TARGET_MODEL) print('Working...') model = load_model(Config.MODEL_DIR + Config.TARGET_MODEL) print('Done.') print('Loading labels from disk: ' + Config.TARGET_LABELS) print('Working...') with open(Config.DATA_DIR + Config.TARGET_LABELS, mode='rb') as f: labels = pickle.load(f) print('Done.') print('Loading historical articles...') nyt_article_archive = json.load( open('../data/NYT_Articles_October_1998.json', 'r')) article_list = nyt_article_archive['response']['docs'] for article in article_list: article_date = parse(article['pub_date']) articles_for_day[article_date.day].append({ 'headline': article['headline']['main'], 'content': article['lead_paragraph'] })
def process(image, model) : feature_file = get_features(image) file = open('feature.txt', 'w') file.write(feature_file) file.close() predict_image('feature.txt', load_model(model))
def feval(streamf): z = load_model(".\\Model\\model.cmf") input_map = { z.arguments[0]: streamf.streams.features, } dat1 = streamf.next_minibatch(1000, input_map=input_map) output = z.eval(dat1) for i in range(len(output)): print(output[i])
def lstm(qry, ans, ans2): # Get the model query_vector = load_model("models/query_vector.model") answer_vector = load_model("models/answer_vector.model") # Get the data file value = getVarFromFile("variables.txt") queryf = open(value.query_wf) ansf = open(value.answer_wf) query_wl = [line.rstrip('\n') for line in queryf] answers_wl = [line.rstrip('\n') for line in ansf] query_dict = {query_wl[i]: i for i in range(len(query_wl))} answers_dict = {answers_wl[i]: i for i in range(len(answers_wl))} queryf.close() ansf.close() # let's run a sequence through qry_idx = [query_dict[w + ' '] for w in qry.split()] # convert to query word indices ans_idx = [answers_dict[w + ' '] for w in ans.split()] # convert to answer word indices ans2_idx = [answers_dict[w + ' '] for w in ans2.split()] # convert to fake answer word indices # Create the one hot representations qry_onehot = np.zeros([len(qry_idx), len(query_dict)], np.float32) for t in range(len(qry_idx)): qry_onehot[t, qry_idx[t]] = 1 ans_onehot = np.zeros([len(ans_idx), len(answers_dict)], np.float32) for t in range(len(ans_idx)): ans_onehot[t, ans_idx[t]] = 1 ans2_onehot = np.zeros([len(ans2_idx), len(answers_dict)], np.float32) for t in range(len(ans2_idx)): ans2_onehot[t, ans2_idx[t]] = 1 qry_embedding = query_vector.eval([qry_onehot]) ans_embedding = answer_vector.eval([ans_onehot]) ans2_embedding = answer_vector.eval([ans2_onehot]) qry_ans_similarity = 1 - cosine(qry_embedding, ans_embedding) qry_ans2_similarity = 1 - cosine(qry_embedding, ans2_embedding) return qry_ans_similarity, qry_ans2_similarity
def init(): global trainedModel, mem_after_init start = t.default_timer() # Load the model from disk and perform evals trainedModel = load_model('resnet.dnn') end = t.default_timer() loadTimeMsg = "Model loading time: {0} ms".format(round((end-start)*1000, 2)) logger.info(loadTimeMsg)
def test_load_save_constant(tmpdir): c = constant(value=[1, 3]) root_node = c * 5 result = root_node.eval() expected = [[[[5, 15]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'c_plus_c.mod') root_node.save_model(filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval() assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval() assert np.allclose(loaded_result, expected)
def test_load_save_constant(tmpdir): c = constant(value=[1,3]) root_node = c * 5 result = root_node.eval() expected = [[[[5,15]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'c_plus_c.mod') root_node.save_model(filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval() assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval() assert np.allclose(loaded_result, expected)
def init(): global trainedModel, mem_after_init start = t.default_timer() # Load the model from disk and perform evals trainedModel = load_model('resnet.dnn') end = t.default_timer() loadTimeMsg = "Model loading time: {0} ms".format( round((end - start) * 1000, 2)) logger.info(loadTimeMsg)
def feval(streamf): z = load_model(".\\Model\\model.cmf") input_map = { z.arguments[0]: streamf.streams.spread, z.arguments[1]: streamf.streams.high, z.arguments[2]: streamf.streams.low, z.arguments[3]: streamf.streams.volume } dat1 = streamf.next_minibatch(1000, input_map=input_map) output = z.eval(dat1) for i in range(len(output)): print(output[i])
def load_and_sample(model_filename, vocab_filename, prime_text='', use_hardmax=False, length=1000, temperature=1.0): # load the model model = load_model(model_filename) # load the vocab vocab_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), vocab_filename) chars = [c[0] for c in open(vocab_filepath).readlines()] char_to_ix = { ch:i for i,ch in enumerate(chars) } ix_to_char = { i:ch for i,ch in enumerate(chars) } return sample(model, ix_to_char, len(chars), char_to_ix, prime_text=prime_text, use_hardmax=use_hardmax, length=length, temperature=temperature)
def test_load_save_unique_input(tmpdir): i1 = input_variable((1,2), name='i1') root_node = softmax(i1) input1 = [[[1,2]]] result = root_node.eval(input1) expected = [[[[ 0.268941, 0.731059]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'i_plus_0.mod') root_node.save_model(filename) loaded_node = load_model(filename) # Test specifying the only value for an unique input loaded_result = loaded_node.eval(input1) assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval(input1) assert np.allclose(loaded_result, expected)
def test_load_save_unique_input(tmpdir): i1 = input_variable((1, 2), name='i1') root_node = softmax(i1) input1 = [[[1, 2]]] result = root_node.eval(input1) expected = [[[[0.268941, 0.731059]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'i_plus_0.mod') root_node.save_model(filename) loaded_node = load_model(filename) # Test specifying the only value for an unique input loaded_result = loaded_node.eval(input1) assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval(input1) assert np.allclose(loaded_result, expected)
def test_load_save_input_legacy_names(tmpdir): i1 = input_variable((1,2), name='i1') root_node = abs(i1) input1 = [[[-1,2]]] result = root_node.eval({i1: input1}) expected = [[[[1,2]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'i_plus_c_0.mod') root_node.save_model(filename) loaded_node = load_model(filename) # Test specifying the input node names by order loaded_result = loaded_node.eval([input1]) assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval([input1]) assert np.allclose(loaded_result, expected)
def test_load_save_input(tmpdir): i1 = input_variable((1, 2), name='i1') root_node = abs(i1) input1 = [[[-1, 2]]] result = root_node.eval({i1: input1}) expected = [[[[1, 2]]]] assert np.allclose(result, expected) filename = str(tmpdir / 'i_plus_c_0.mod') root_node.save_model(filename) loaded_node = load_model(filename) # Test spefying the input node names by order loaded_result = loaded_node.eval([input1]) assert np.allclose(loaded_result, expected) filename = filename + '.legacy' save_as_legacy_model(root_node, filename) loaded_node = load_model(filename) loaded_result = loaded_node.eval([input1]) assert np.allclose(loaded_result, expected)
def load_and_sample(model_filename, vocab_filename, prime_text='', use_hardmax=False, length=1000, temperature=1.0): # load the model model = load_model(model_filename) # load the vocab chars = [c[0] for c in open(vocab_filename).readlines()] char_to_ix = { ch:i for i,ch in enumerate(chars) } ix_to_char = { i:ch for i,ch in enumerate(chars) } output = sample(model, ix_to_char, len(chars), char_to_ix, prime_text=prime_text, use_hardmax=use_hardmax, length=length, temperature=temperature) ff = open('output.txt', 'w', encoding='utf-8') ff.write(output) ff.close()
def test_lm(config): model_file_path = config.model_file_path model = load_model(model_file_path) result_file = open('result.txt', 'w') token_to_id_path = config.token_to_id_path slot_value_path = config.slot_value_path segment_begin = config.segment_begin segment_end = config.segment_end test_file_path = config.test_file_path sv_test_file_path = config.sv_test_file_path with open(test_file_path,'r') as f: test_lines = f.readlines() with open(sv_test_file_path,'r') as f: sv_test_lines = f.readlines() sequence_length = config.sequence_length sequences_per_batch = config.sequences_per_batch hidden_dim = config.hidden_dim overgen = config.overgen beamwidth = config.beamwidth decode = config.decode data = DataReader('', '', segment_begin, segment_end, token_to_id_path, slot_value_path) begin_sentence_id = data.segment_begin_id end_sentence_id = data.segment_end_id id_to_token = data.id_to_token vocab_dim = data.vocab_dim sv_dim = data.sv_dim for features, svs, hh, cc, labels, token_count in data.minibatch_generator(test_lines, sv_test_lines, sequence_length, sequences_per_batch, hidden_dim, sv_dim): cnt = 0 while cnt < len(features): # just input the first vector, begin sentence vector fe1 = [np.array([features[cnt][0].tolist()], dtype=np.float32)] sv1 = [np.array([svs[cnt].tolist()], dtype=np.float32)] hh1 = [np.array([hh[cnt].tolist()], dtype=np.float32)] cc1 = [np.array([cc[cnt].tolist()], dtype=np.float32)] if decode == 'beam': result = beamsearch(model, vocab_dim, 100, fe1, hh1, cc1, sv1, id_to_token, overgen, beamwidth, begin_sentence_id, end_sentence_id) for res in result: result_file.write(res.strip() + '\n') cnt += 1 result_file.close()
def _load_models(self): self._target_net = load_model('trainedModels/Target_net') self._action_value_net = load_model('trainedModels/Action_vale_net') print('Action value and Target model loaded')
def Recategorize(): print('startingRecategorization') #f = open(output,'a+') #reader_test = create_reader(os.path.join(data_path, 'Animals.txt'), os.path.join(data_path, 'CIFAR-10_mean.xml'), False) Paths = [ "E:/img/Animals2/Rabbit/", "E:/img/Animals2/Racoon/", "E:/img/Animals2/Skunk/", "E:/img/Animals2/Weasel/", "E:/img/Animals2/Bobcat/", "E:/img/Animals2/Cat/", "E:/img/Animals2/Chicken/", "E:/img/Animals2/Coyote/", "E:/img/Animals2/Deer/", "E:/img/Animals2/Dog/", "E:/img/Animals2/Duck/", "E:/img/Animals2/Eagle/", "E:/img/Animals2/Hawk/", "E:/img/Animals2/MountainLion/", "E:/img/Animals2/Owl/", "E:/img/Animals2/Possum/", "E:/img/Animals2/Robin/", "E:/img/Animals2/Squirrel/", "E:/img/Animals2/Woodpecker/", "E:/img/Animals2/Humans/", "E:/img/Animals2/BlueJay/", "E:/img/Animals2/Backgrounds/", "E:/img/Animals2/Bear/" ] #Paths = ["E:/img/Animals2/Backgrounds/"] CategoriesPass = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] CategoriesFail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] MisCategoriesFail = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ind = 0 j = 0 z = load_model( "E:/img/Animals2/Models/ConvNet_CIFAR10_DataAugModelEvolveModel.dnn") for mypath in Paths[0:23]: print('path:' + mypath) onlyfiles = [fl for fl in listdir(mypath) if fl.endswith('.jpeg')] length = len(onlyfiles) i = 0 for fl2 in onlyfiles: #decide = fl2.split('_') #fl1 = fl2.replace('2.bmp','1.bmp') #length2 = fl2.index(".") +2 #manipulate path to pull 2 images and turn into label and features. #flName = fl2[:length2] #ind = partName.index(flName) # label = indexLabels(ind,25) # labels = "%s" %label # labels = labels.replace(',', '') # labels = labels.replace('[', '') # labels = labels.replace(']', '') #pth1 =mypath+fl1 pth2 = mypath + fl2 # img_vec1 = flattenImages(pth2) # features = "%s" %img_vec1 # features = features.replace(',', '') # features = features.replace('[', '') # features = features.replace(']', '') #img_vec2 = flattenImagesBW(pth2,directory + 'S4b/'+fl2) #unflat = unflattenImagesBW(img_vec2,100,100) #unflat.save(directory + 'Sout/'+fl2) #outputs = "%s" %img_vec2 #outputs = outputs.replace(',', '') #outputs = outputs.replace('[', '') #outputs = outputs.replace(']', '') # f.write('|labels ') # f.writelines(labels) #resizeImage(pth2,directory + "Animals/" + fl2) #img = Image.open(directory + "Animals/" + fl2) #pic = np.array(img, dtype=np.float32) #img.show() #pic = np.ascontiguousarray(np.transpose(pic, (2, 0, 1))) #print (pic) pic = flattenImages(mypath + fl2) #evalu = z.eval(pic) #print (evalu) predictions = np.squeeze(z.eval(pic)) top_class = np.argmax(predictions) #print (mypath,top_class,predictions) if top_class == ind: #img = pic.reshape([100,100,3]) #imgplot = plt.imshow(img) #print(Paths[top_class]) #print ('count: ', i) #plt.ion() #plt.pause(0.2) #plt.show() #plt.close() #else: #Percent = 100*((predictions[ind])/(sum(predictions[0:21]))) #print("Success!!!!!!!!!!!!!!!!!!!!!!!!!!", Percent) #print (mypath,top_class,predictions) #print(pth2) CategoriesPass[ind] = CategoriesPass[ind] + 1 else: #Percent = 100*((predictions[ind])/(sum(predictions[0:23]))) #PercentT =100*((predictions[top_class])/(sum(predictions[0:15]))) #print("Fail!", Percent, PercentT, Paths[top_class]) CategoriesFail[ind] = CategoriesFail[ind] + 1 MisCategoriesFail[top_class] = MisCategoriesFail[top_class] + 1 if (predictions[ind] <= 0): print("SuperFail!", mypath, predictions, predictions[ind]) #img = Image.open(mypath + fl2) #move really bad to processing try: os.rename(pth2, Paths[top_class] + fl2) except: continue #img.save("E:/img/Animals/Rejected/" + Paths[top_class][15:18] + fl2 ) #imgplot = plt.imshow(img) #imgplot.fname = fl2 print(Paths[top_class]) print('count: ', i) #plt.ion() #plt.pause(.1) #plt.show() #plt.close() i = i + 1 j = j + 1 ind = ind + 1 print(CategoriesPass) print(CategoriesFail) print(MisCategoriesFail) print('done')
def init(): global model # Load CNTK model model = load_model("model/solar_2000ep.dnn")
def __init__( self, input_shape, nb_actions, gamma=0.99, explorer=LinearEpsilonAnnealingExplorer( 1, 0.1, 10), #start, end, steps 100000 learning_rate=0.00025, momentum=0.95, minibatch_size=16, memory_size=500000, train_after=10000, train_interval=4, target_update_interval=10000, monitor=True): self.input_shape = input_shape self.nb_actions = nb_actions self.gamma = gamma self._train_after = train_after self._train_interval = train_interval self._target_update_interval = target_update_interval self._explorer = explorer self._minibatch_size = minibatch_size self._history = History(input_shape) self._memory = ReplayMemory(memory_size, input_shape[1:], 4) self._num_actions_taken = 0 # Metrics accumulator self._episode_rewards, self._episode_q_means, self._episode_q_stddev = [], [], [] # Action Value model (used by agent to interact with the environment) with default_options(activation=relu, init=he_uniform()): self._action_value_net = Sequential([ Convolution2D((8, 8), 16, strides=4), Convolution2D((4, 4), 32, strides=2), Convolution2D((3, 3), 32, strides=1), Dense(256, init=he_uniform(scale=0.01)), Dense(nb_actions, activation=None, init=he_uniform(scale=0.01)) ]) #dense 256 self._action_value_net.update_signature(Tensor[input_shape]) # Target model used to compute the target Q-values in training, updated # less frequently for increased stability. self._target_net = self._action_value_net.clone(CloneMethod.freeze) # Function computing Q-values targets as part of the computation graph @Function @Signature(post_states=Tensor[input_shape], rewards=Tensor[()], terminals=Tensor[()]) def compute_q_targets(post_states, rewards, terminals): return element_select( terminals, rewards, gamma * reduce_max(self._target_net(post_states), axis=0) + rewards, ) # Define the loss, using Huber Loss (more robust to outliers) @Function @Signature(pre_states=Tensor[input_shape], actions=Tensor[nb_actions], post_states=Tensor[input_shape], rewards=Tensor[()], terminals=Tensor[()]) def criterion(pre_states, actions, post_states, rewards, terminals): # Compute the q_targets q_targets = compute_q_targets(post_states, rewards, terminals) # actions is a 1-hot encoding of the action done by the agent q_acted = reduce_sum(self._action_value_net(pre_states) * actions, axis=0) # Define training criterion as the Huber Loss function return huber_loss(q_targets, q_acted, 1.0) # Adam based SGD lr_schedule = learning_rate_schedule(learning_rate, UnitType.minibatch) m_schedule = momentum_schedule(momentum) vm_schedule = momentum_schedule(0.999) l_sgd = adam(self._action_value_net.parameters, lr_schedule, momentum=m_schedule, variance_momentum=vm_schedule) self._metrics_writer = TensorBoardProgressWriter( freq=1, log_dir='metrics', model=criterion) if monitor else None self._learner = l_sgd self._trainer = Trainer(criterion, (criterion, None), l_sgd, self._metrics_writer) # if os.path.isfile('./cnnNetwork_CheckpointRightGoal.dnn'): # if os.path.isfile('./cnnNetwork_Checkpoint.dnn'): #if os.path.isfile('./cnnNetwork_CheckpointLeftGoal.dnn') if os.path.isfile('./savednetwork.dnn'): self._action_value_net = load_model("./savednetwork.dnn") self._target_net = self._action_value_net.clone(CloneMethod.freeze)
import time import argparse from IPython.display import Image as display from resizeimage import resizeimage size = 32 label_lookup = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] #z = load_model('./VGG Models/pred_vgg9_300_400.dnn') #z = load_model('./VGG Models/pred_vgg9.dnn') z = load_model('./pred_vgg9_300_400.dnn') arg_parser = argparse.ArgumentParser() arg_parser.add_argument("-d", "--image_dir", type=str, default="./images", required=True, help="the directory to get test images") args = vars(arg_parser.parse_args()) images_dir = f"{args['image_dir']}" print(f"Image Directory: {images_dir}")
from cntk.ops.functions import load_model from PIL import Image import numpy as np modelName = "ConvNet_mytrain_29.dnn" z = load_model(modelName) ######################### count_total = 0 count_correct = 0 for type0 in range(1, 31): rgb_image = np.asarray(Image.open("0/" + str(type0) + ".png"), dtype=np.float32) - 128 bgr_image = rgb_image[..., [2, 1, 0]] pic = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) predictions = np.squeeze(z.eval({z.arguments[0]: [pic]})) top_class = np.argmax(predictions) print("This is predictions") print(predictions) print("Most possible class for \"file name :" + "0/" + str(type0) + ".png" + "\"") print(top_class) if (top_class == 0): count_correct += 1 count_total += 1 for type1 in range(1, 31): rgb_image = np.asarray(Image.open("1/" + str(type1) + ".png"), dtype=np.float32) - 128 bgr_image = rgb_image[..., [2, 1, 0]]
from cntk.ops.functions import load_model from PIL import Image import time import argparse from IPython.display import Image as display from resizeimage import resizeimage size = 32 label_lookup = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] z = load_model('./pred_vgg9.dnn') arg_parser = argparse.ArgumentParser() arg_parser.add_argument("-d", "--image_dir", type=str, default="./", required=True, help="the directory to get test images") args = vars(arg_parser.parse_args()) images_dir = f"{args['image_dir']}" print(f"Image Directory: {images_dir}")
import os import json import numpy as np from cntk.ops.functions import load_model model = load_model("D:/home/site/wwwroot/trained_model") postreqdata = json.loads(open(os.environ['req']).read()) a = postreqdata["a"] b = postreqdata["b"] result = model.eval({model.arguments[0]: [[a, b]]}) print(result) response = open(os.environ['res'], 'w') response.write(str(np.argmax(result))) response.close()
# random words instead of prompting. def get_token(token2id, token_id): return list(token2id.keys())[list(token2id.values()).index(token_id)] def get_closest_words(sorted_indices, token2id, i, limit): words = [] for j in range(limit): words.append(get_token(token2id, sorted_indices[i][j])) return words z = load_model(os.path.join(os.getcwd(), "word2vec_checkpoint")) weights = z.E.value vocab_dim = weights.shape[0] max_test_words = 20 #vocab_dim max_closest_words = 10 training_data = pickle.load(open('tmp_textdata.pickle', 'rb')) token2id = training_data.token2id id2token = {v: k for k, v in token2id.items()} # Reduce memory usage by deleting the unused full text training_data.text_as_id_list = [] #Calculate Euclidean distance between word vectors
from cntk.ops.functions import load_model from PIL import Image import numpy as np z = load_model("../../../my-2/Models/resnet20_5.dnn") rgb_image = np.asarray(Image.open("123448.png"), dtype=np.float32) - 128 bgr_image = rgb_image[..., [2, 1, 0]] pic = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) predictions = np.squeeze(z.eval({z.arguments[0]: [pic]})) top_class = np.argmax(predictions) print(top_class)
log_content = log_content + f.read() f.close() #print(log_content) # Extract the validation errors of all epochs result = re.findall('Finished Evaluation \[(\d+)\]: Minibatch\[\d+\-\d+\]: metric = (\d+.\d+)', log_content, flags=re.DOTALL) # Find out the best epoch best_model = min(result, key=lambda x: float(x[1])) best_model = int(best_model[0]) ############################# # Load model # ############################# model = load_model(Models_PathAbs % best_model) features = model.find_by_name('features') ############################# # Read input data # ############################# reader = MinibatchSource(create_feature_deserializer(FeatureTest_PathAbs), trace_level = 1, randomize = False) # Define mapping from reader stream to network input input_map = {} input_map[features] = reader.streams.features ############################# # Prediction # #############################
import os, sys #from CNN import eval from cntk.ops.functions import load_model from PIL import Image import scipy.misc import cv2 import time from IPython.display import Image as display label_lookup = [ "airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck" ] z = load_model('../../../DNN_Project_Implementation\pred_vgg9_300_400.dnn') #print(z.architecture()) #img_file = resizeIt('Car.png') #Car_3.png rgb_image = np.asarray(Image.open("_Car_small.jpg"), dtype=np.float32) - 128 bgr_image = rgb_image[..., [2, 1, 0]] pic = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) predictions = np.squeeze(z.eval({z.arguments[0]: [pic]})) top_class = np.argmax(predictions) #print("\tTOP CLASS Label: {:10s}, confidence: {:.2f}%".format(label_lookup[top_class], predictions[top_class] * 100)) #FPS:134.04600493493618 can be processed
from cntk.ops.functions import load_model from PIL import Image import numpy as np z = load_model("ConvNet_mytrain_29.dnn") rgb_image = np.asarray(Image.open("img.png"), dtype=np.float32) - 128 ######################### bgr_image = rgb_image[..., [2, 1, 0]] pic = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) predictions = np.squeeze(z.eval({z.arguments[0]: [pic]})) top_class = np.argmax(predictions) print("This is predictions") print(predictions) print("Most possible class") print(top_class)
def init(): global MODEL MODEL = load_model(MODEL_PATH)
import sys from path_helper import * userDirectory = UserDirectory("CarRegistrationPlateDetector") #projectName = "CarRegistrationPlate" ModelFileName = "faster_rcnn_eval_AlexNet_e2e.model" imageSize = (850, 850) modelPath = os.path.abspath(os.path.join(".", "Output", ModelFileName)) print (modelPath) model = load_model(modelPath) imagesDir = userDirectory.getImageDir("positive") print ("Searching for images in " + imagesDir) imageFiles = getFilesInDirectoryByType(imagesDir, ["jpg"]) #for imageFileName in sys.argv: for imageFileName in imageFiles: print ("Processing file: " + imageFileName) imageFileName = os.path.join(imagesDir, imageFileName) inMemImage = Image.open(imageFileName) inMemImage = inMemImage.resize(imageSize) rgb_image = np.asarray(inMemImage, dtype=np.float32) - 128