def generate_logs(model, logdir): ''' Generate logs for a model for graph visualization in Tensorboard. ''' with tf.Session() as sess: load.load_model(model) train_writer = tf.summary.FileWriter(logdir) train_writer.add_graph(sess.graph)
def main_rain(): drY = np.identity(10) X = init_inputs((10, 1, 28, 28)) # trX, teX, trY, teY = mnist(onehot=True) # # trX = trX.reshape(-1, 1, 28, 28) # teX = teX.reshape(-1, 1, 28, 28) # X = T.ftensor4() Y = T.fmatrix() params = load_model('media/model/conv-final.model') l1, l2, l3, l4, py_x = model(X, params, 0., 0.) cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y)) updates = RMSprop(cost, [X], lr=0.001) dream = theano.function(inputs=[Y], outputs=cost, updates=updates, allow_input_downcast=True) ss = None costs = [] for i in range(100): for j in range(100): cost = dream(drY) costs.append(cost) print i, cost xv = X.get_value() s = None for xvi in xv: xvi.resize(28, 28) if s is None: s = np.array(xvi, copy=True) else: s = np.concatenate((s, xvi)) if ss is None: ss = np.array(s, copy=True) else: ss = np.concatenate((ss, s), axis=1) im = Image.fromarray(s * 255) name = 'media/dream/conv/{0}.png'.format(str(i)) im = im.convert('RGB') im.save(name) im = Image.fromarray(ss * 255) name = 'media/dream/conv/all.png' im = im.convert('RGB') im.save(name)
def main_rain(): drY = np.identity(10) X = init_inputs((10,1,28,28)) # trX, teX, trY, teY = mnist(onehot=True) # # trX = trX.reshape(-1, 1, 28, 28) # teX = teX.reshape(-1, 1, 28, 28) # X = T.ftensor4() Y = T.fmatrix() params = load_model('media/model/conv-final.model') l1, l2, l3, l4, py_x = model(X, params, 0., 0.) cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y)) updates = RMSprop(cost, [X], lr=0.001) dream = theano.function(inputs=[Y], outputs=cost, updates=updates, allow_input_downcast=True) ss = None costs = [] for i in range(100): for j in range(100): cost = dream(drY) costs.append(cost) print i, cost xv = X.get_value() s = None for xvi in xv: xvi.resize(28, 28) if s is None: s = np.array(xvi, copy=True) else: s = np.concatenate((s, xvi)) if ss is None: ss = np.array(s, copy=True) else: ss = np.concatenate((ss, s), axis=1) im = Image.fromarray(s * 255) name = 'media/dream/conv/{0}.png'.format(str(i)) im = im.convert('RGB') im.save(name) im = Image.fromarray(ss * 255) name = 'media/dream/conv/all.png' im = im.convert('RGB') im.save(name)
def predict(): ''' For rendering results on HTML GUI ''' msg1 = request.form["experience"] # pass this through tokenizer first model = load_model() tokenizer = load_tokenizer() #int_features = [int(x) for x in request.form.values()] #final_features = [np.array(int_features)] # prediction = model.predict(msg1) pred = predict_spam([msg1], tokenizer, model) #return render_template('index.html', prediction_text='Your message is '+pred) return render_template('index.html', pred_value=pred)
def main_train(): drY = np.identity(10) X = init_inputs((10, 784)) Y = T.fmatrix() params = load_model('media/model/net-final.model') py_x = model(X, params) cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y)) updates = sgd(cost, [X]) dream = theano.function(inputs=[Y], outputs=cost, updates=updates, allow_input_downcast=True) ss = None costs = [] for i in range(100): for j in range(100): cost = dream(drY) costs.append(cost) print i, cost xv = X.get_value() s = None for xvi in xv: xvi.resize(28, 28) if s is None: s = np.array(xvi, copy=True) else: s = np.concatenate((s, xvi)) if ss is None: ss = np.array(s, copy=True) else: ss = np.concatenate((ss, s), axis=1) im = Image.fromarray(s * 255) name = 'media/dream/net/{0}.png'.format(str(i)) im = im.convert('RGB') im.save(name) im = Image.fromarray(ss * 255) name = 'media/dream/net/all.png' im = im.convert('RGB') im.save(name)
import sys import math from load import load_model lambda1 = 0.95 lambda_unk = 1 - lambda1 V = 1000000.0 W = 0.0 H = 0.0 unk = 0 model = load_model(sys.argv[1]) # test the model with open('test.txt', 'r') as test_file: for line in test_file: words = line.split() words.append('</s>') for word in words: W += 1 P = lambda_unk / V if model.get(word): P += lambda1 * model[word] else: unk += 1 H += -(math.log(P, 2)) print 'entropy = {}'.format(H/W) print 'coverage = {}'.format((W - unk)/W)
def __init__(self, model, processor): """Initialises image variables and loads model""" self.img_wid = 128 self.img_hei = 72 self.device, self.net = load_model(model, processor)
# coding=utf-8 # Created by Wang yu at 2021/3/12 import io import sys import torch from torchvision import transforms from PIL import Image sys.path.append("model_dir/20210312") from load import load_model device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = load_model("model_dir/20210312/model_03111848_180.pth", device) model.to(device) # Since we are using our model only for inference, switch to `eval` mode: model.eval() def transform_image(image_bytes): my_transforms = transforms.Compose([transforms.Resize((320, 480)), transforms.ToTensor(), transforms.Normalize( [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) image = Image.open(io.BytesIO(image_bytes)).convert('RGB') return my_transforms(image).unsqueeze(0) def get_prediction(image_bytes): tensor = transform_image(image_bytes=image_bytes).to(device)
plt.subplot(len(grids_with_varying) + 1, 1, 1) plt.axis('off') plt.imshow(to_image(original, False)) for i, grid_with_varying in enumerate(grids_with_varying): plt.subplot(len(grids_with_varying) + 1, 1, i + 2) plt.axis('off') plt.imshow(to_image(grid_with_varying, False)) if out_file is not None: plt.savefig(out_file, dpi=200) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Random Path GAN visualization') parser.add_argument('--model', type=str, help='model weights') parser.add_argument('--model_params', type=str, help='model parameters json') parser.add_argument('--out_dir', type=str, help='out directory') parser.add_argument('--count', type=int, default=3, help='images to generate') parser.add_argument('--device', type=int, default=0, help='cuda device to use') args = parser.parse_args() torch.cuda.set_device(args.device) if not os.path.isdir(args.out_dir): os.makedirs(args.out_dir) generator = load_model(args.model, args.model_params) for i in range(args.count): inspect_path_generator_freeze( generator, out_file=os.path.join(args.out_dir, 'rpgan_inspection_{}.png'.format(i + 1)))
action="store", dest="category_names", default="cat_to_name.json") parser.add_argument('--gpu', action="store_true", dest="gpu", default=False) results = parser.parse_args() #print('input = {!r}'.format(results.input)) #print('checkpoint = {!r}'.format(results.checkpoint)) #print('topk = {!r}'.format(results.topk)) #print('category names = {!r}'.format(results.category_names)) print('gpu = {!r}'.format(results.gpu)) model = load.load_model(results.checkpoint) if results.gpu: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model.to(device) #criterion = load.load_criterion(results.checkpoint) #loaded_optimizer = load_optimizer('checkpoint.pth', loaded_model) class_to_idx = load_class_to_idx(results.checkpoint) idx_to_class = {v: k for k, v in class_to_idx.items()} with open(results.category_names, 'r') as f: cat_to_name = json.load(f) image_path = results.input (classes, probs) = predict(image_path, model, results.topk)
# val = predict(img, pretrained_model) # print(val) def sanity(img): labels = predict(img, pretrained_model) plt.figure(figsize=(5,10)) ax = plt.subplot(2,1,1) img - process_image(img) imshow(img,ax) sns.set_style("whitegrid") sns.subplot(2,1,2) sns.barplot(x = labels['predictions'], y = labels['flower_name'], color = '#047495') plt.xlabel("Probability of Prediction") plt.xlabel("") plt.show(); sns.set_style("white") # model = load.model() # print(model) if gpu_u: checkpoint = load.load_model(checkpoint) arch_name = checkpoint['structure'] labels = predict(inputdir,pretrained_model,flower_to_name,topk_value) # print(labels) else: pretrained_model = load.load_model_without_cpu(checkpoint) # pretrained_model = train.pretrained_model labels = predict(inputdir,pretrained_model,flower_to_name,topk_value) print(labels)
If so, please report your hyperparameters. You'll find the results, log and video recordings of your agent every 250k under the corresponding file in the results folder. A good way to monitor the progress of the training is to use Tensorboard. The starter code writes summaries of different variables. To launch tensorboard, open a Terminal window and run tensorboard --logdir=results/ Then, connect remotely to address-ip-of-the-server:6006 6006 is the default port used by tensorboard. """ if __name__ == '__main__': # make env env = gym.make(config.env_name) env = MaxAndSkipEnv(env, skip=config.skip_frame) env = PreproWrapper(env, prepro=greyscale, shape=(80, 80, 1), overwrite_render=config.overwrite_render) # load model model = NatureQN(env, config) model.initialize() loaded = load_model(model) assert loaded != False, "Loading failed" # evaluate one episode of data model.evaluate(env, 1)