def get_next_batch(batch_size=128): codes = [] images = [] max_width_image = 0 for i in range(batch_size): font_name = random.choice(FontNames) font_length = random.randint(50, 100) font_size = random.randint(9, 20) text, image= getImage(CHARS, font_name, image_height, font_length, font_size) images.append(image) if image.shape[1] > max_width_image: max_width_image = image.shape[1] text_list = [CHARS.index(char) for char in text] codes.append(text_list) inputs = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(images)): image_vec = img2vec(images[i], height=image_height, width=max_width_image, flatten=False) inputs[i,:] = np.transpose(image_vec) labels = [np.asarray(i) for i in codes] #labels转成稀疏矩阵 sparse_labels = sparse_tuple_from(labels) seq_len = np.ones(batch_size) * max_width_image return inputs, sparse_labels, seq_len
def filter3(patch, images): patches = [] for cell in patch.cells: image = utils.getImage(cell[0], images) C = [ image.cells[cell[1][1]][cell[1][0]], image.cells[cell[1][1] + 1][cell[1][0]], image.cells[cell[1][1] - 1][cell[1][0]], image.cells[cell[1][1]][cell[1][0] + 1], image.cells[cell[1][1]][cell[1][0] - 1] ] for c in C: for p in c.q: patches.append(p) pos = 0 for p in patches: if utils.isNeighbour(patch, p, 2): pos += 1 ratio = pos / len(patches) * 100 if ratio < 0.25: return True return False
def imageReader(Q): for i in trange(segImages.nImages): row = segImages.DF.iloc[i] image, _ = getImage(segImages.array, row) #image = numpy.zeros((750, 750, 750)) Q.put((row,image)) Q.put(None)
def add_from_single_image(self, image_path, example_width, example_height, label, count, imgs_per_row): """ imports training examples from a single image image has to consist of all examples next to each other, row by row Args: image_path: path to image example_width: width of examples in pixels example_height: height of examples in pixels label: labels of imported data (array with size 2) count: number of examples to import imgs_per_row: number of examples per row in the image """ src_image = utils.getImage(image_path) x = 0 y = 0 for i in xrange(count): example = src_image[y : y + example_height, x : x + example_width] if example_height != self.img_size[1] or example_width != self.img_size[0]: example = utils.scaleImage(example, self.img_size) # add dataset self.images = numpy.append(self.images, example) self.labels = numpy.append(self.labels, label) self.count = self.count + 1 x += example_width if x >= imgs_per_row * example_width: x = 0 y += example_height del src_image
def get_next_batch(batch_size=128): codes = [] images = [] max_width_image = 0 font_min_length = random.randint(10, 80) for i in range(batch_size): font_name = random.choice(FontNames) font_length = random.randint(font_min_length-5, font_min_length+5) font_size = random.randint(14, 64) text, image= getImage(CHARS, font_name, image_height, font_length, font_size, eng_world_list) images.append(image) if image.shape[1] > max_width_image: max_width_image = image.shape[1] text_list = [CHARS.index(char) for char in text] codes.append(text_list) # 凑成4的整数倍 max_width_image = max_width_image + (POOL_SIZE - max_width_image % POOL_SIZE) inputs = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(images)): image_vec = img2vec(images[i], height=image_height, width=max_width_image, flatten=False) inputs[i,:] = np.transpose(image_vec) labels = [np.asarray(i) for i in codes] #labels转成稀疏矩阵 sparse_labels = sparse_tuple_from(labels) #因为模型做了2次pool,所以 seq_len 也需要除以4 seq_len = np.ones(batch_size) * (max_width_image * image_height) // (POOL_SIZE * POOL_SIZE) return inputs, sparse_labels, seq_len
def registerPatch(patch, Vp, VpStar, beta, f, fprime, isDisplay) : for image in Vp : pmat = image.pmat pt = pmat @ patch.center pt /= pt[2] x = int(pt[0]/beta) y = int(pt[1]/beta) image.cells[y][x].q.append(patch) isQStar = 0 if utils.getImage(image.id, VpStar) : isQStar = 1 image.cells[y][x].qStar.append(patch) patch.VpStar.append(image) utils.removeFeatures(image, image.cells[y][x]) cell = np.array([image.id, [x, y], isQStar, 0]) patch.cells.append(cell) patch.Vp.append(image) if isDisplay : ref = cv.imread(patch.ref.name) cv.circle(ref, (int(f.x), int(f.y)), 3, (0, 255, 0), -1) img = image.displayFeatureMap() cv.circle(img, (int(pt[0]), int(pt[1])), 3, (0, 255, 0), -1) cv.imshow(f'Ref : {patch.ref.id}', ref) cv.imshow(f'Img : {image.id}', img) cv.waitKey(0) cv.destroyAllWindows()
def add_examples(self, image_path, count, imgs_per_row, label): src_image = utils.getImage(image_path) x = 0 y = 0 for i in xrange(count): example = src_image[y : y + self.img_size[1], x : x + self.img_size[0]] for j in xrange(4): example = utils.rotateImage(example, 90) # add normal self.images.append(example) # add inverted self.images.append(1.0 - example) if not label is None: # add label self.labels.append(label) # add label for inverted self.labels.append(label) self.count += 2 x += self.img_size[0] if x >= imgs_per_row * self.img_size[0]: x = 0 y += self.img_size[1] del src_image
def imageCubeGen(imageArray, imageDF, noduleDF, candidatesDF, args, atMost=200): cubeSize = args.cubeSize autoencoder = args.autoencoder while True: for _, row in imageDF.iterrows(): image, imageNum = getImage(imageArray, row) imgNodules = noduleDF[noduleDF.imgNum==imageNum] imgCandidates = candidatesDF[candidatesDF.imgNum==imageNum] #if autoencoder: image = numpy.expand_dims(image, axis=2) cubes, positions = getImageCubes(image, cubeSize, prep=True) for cube, pos in zip(cubes, positions): realPos = pos*cubeSize z, y, x = realPos #if random() > 0.4 and cube.mean() < 300: continue # doesn't work! its normalized now # is there a nodule or candidates in this cube? nodulesInCube = findNodules(imgNodules, x, y, z, cubeSize) numNodules = len(nodulesInCube) candidatesInCube = findNodules(imgCandidates, x, y, z, cubeSize) numCandidates = len(candidatesInCube) if (numNodules or numCandidates): print 'Candidates: %s Nodules: %s' % (numCandidates, numNodules) #else: print '-'*10 #if numCandidates: plot_nodule(cube) if len(nodulesInCube): diam = getNoduleDiameter(nodulesInCube.iloc[0]) else: diam = getNoduleDiameter(None) #targets = { # 'nodule': (numNodules>0)*1.0, # #'candidate': (numCandidates>0)*1.0, # 'diam' : diam #} if len(nodulesInCube): noduleRow = nodulesInCube.iloc[0] else: noduleRow = None nodule = (numNodules>0)*1.0 targets = getTargets(nodule, noduleRow, diamType=args.diam) if autoencoder: targets['imgOut'] = cube #print pos, cube.mean(), numNodules, numCandidates yield [imageNum, cube, targets]
def getUrlImage(): global inputPath global outputPath url = imageURL.get() ext = util.getExtension(url) #gets the extendsion of the URL outExt = util.getExtension(outputPath) # gets the extension of the output if imageURL == '': messagebox.showinfo("Error", "No URL to Image !") return elif ext == '': messagebox.showinfo("Error", "Please Have an Extension") return elif ext != outExt: messagebox.showinfo("Error", "Extensions Don't Match") return else: print("Here") util.getImage(url, outputPath) inputPath = outputPath
def tweet(twitch, twitter, title, isPrint, streamer_type, hashtags): # Obter o objecto API api = twitter_OAuth(streamer_type) # Se o streamer não tiver Twitter, usamos o nome da twitch if twitter == "NaN": twitter = twitch.split("/")[-1] else: twitter = "@" + twitter tweet = f"""{emojis["arrow"]} {twitter} está em Live neste momento!{emojis["red_dot"]} {title.replace("#", " - ")} Entra aí: https://www.{twitch} {hashtags}""" # Verificar se streamer tem imagem propria streamer_id = get1StreamerId(twitch.split("/")[-1]) name_img = os.path.join(DIR_IMAGE, streamer_id + ".png") isStreamerImage = os.path.exists(name_img) isImage = False if not isStreamerImage: # Nome do ficheiro de imagem criado # e se conseguiu descarregar a imagem name_img, isImage = getImage(twitch.split("/")[-1]) # Se conseguiu descarregar a imagem e se os streamer permitiu o print if isImage and isPrint: # Enviar tweet com media api.update_with_media(name_img + ".png", status=tweet) # Elminar Imagens os.remove(name_img + ".png") os.remove(name_img + ".jpg") elif isStreamerImage and isPrint: api.update_with_media(name_img, status=tweet) else: api.update_status(tweet) return
def get_next_batch(batch_size=128): images = [] to_images = [] max_width_image = 0 font_min_length = random.randint(10, 20) for i in range(batch_size): font_name = random.choice(FontNames) font_length = random.randint(font_min_length - 5, font_min_length + 5) font_size = random.randint(9, 64) text, image = utils.getImage(CHARS, font_name, image_height, font_length, font_size, eng_world_list) images.append(image) if image.shape[1] > max_width_image: max_width_image = image.shape[1] to_image = utils.renderNormalFontByPIL(ConsolasFont, 64, text) to_image = utils.trim(to_image) w, h = to_image.size _w = round(w * image_height / h) _h = image_height if _w > max_width_image: _w = max_width_image _h = round(h * max_width_image / w) to_image = to_image.resize((_w, _h), Image.ANTIALIAS) to_image = np.asarray(to_image) #to_image=utils.resize(to_image, height=image_height) to_image = utils.img2gray(to_image) to_image = to_image / 255 to_images.append(to_image) inputs = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(images)): image_vec = utils.img2vec(images[i], height=image_height, width=max_width_image, flatten=False) inputs[i, :] = np.transpose(image_vec) labels = np.zeros([batch_size, max_width_image, image_height]) for i in range(len(to_images)): image_vec = utils.img2vec(to_images[i], height=image_height, width=max_width_image, flatten=False) labels[i, :] = np.transpose(image_vec) return inputs, labels
def registerPatch(patch, Vp, VpStar, beta): for img in Vp: pmat = img.pmat pt = pmat @ patch.center pt /= pt[2] x = int(pt[0] / beta) y = int(pt[1] / beta) img.cells[y][x].q.append(patch) isQStar = 0 if utils.getImage(img.id, VpStar): isQStar = 1 img.cells[y][x].qStar.append(patch) patch.VpStar.append(img) img.cells[y][x].hasRecon = True cell = np.array([img.id, [x, y], isQStar, 1]) patch.cells.append(cell) patch.Vp.append(img)
def filter1(patch, images): U = [] for cell in patch.cells: image = utils.getImage(cell[0], images) i_cell = image.cells[cell[1][1]][cell[1][0]] for p in i_cell.q: if utils.isNeighbour(patch, p, 2): continue U.append(p) a = len(patch.VpStar) b = 1 - utils.computeGStar(patch) c = 0 for pi in U: c += 1 - computeGStar(pi) if a * b < c: return True return False
def add_labels(self, image_path, count, imgs_per_row): src_image = utils.getImage(image_path) x = 0 y = 0 for i in xrange(count): label = src_image[y : y + self.lbl_size[1], x : x + self.lbl_size[0]] for j in xrange(4): label = utils.rotateImage(label, 90) # add label twice for inverted image self.labels.append(label.flatten('C')) self.labels.append(label.flatten('C')) x += self.lbl_size[0] if x >= imgs_per_row * self.lbl_size[0]: x = 0 y += self.lbl_size[1] del src_image
def testAndVisualize(DF, array): DF = DF[DF.cancer==1].head(1) row = DF.iloc[0] image, imgNum = getImage(array, row) imgShape = numpy.asarray(image.shape) bigCam = makeCamImgFromImage(image, cubeSize) # now the cam image and source image are roughly aligned to eachother paddedCamImg, resizedSourceImage = makeResizedSourceImage(bigCam, image) affine = numpy.eye(4) vol2Nifti(paddedCamImg, 'cam.nii.gz', affine=affine) vol2Nifti(resizedSourceImage, 'simg.nii.gz', affine=affine) numpy.save('cam.npy', bigCam) numpy.save('simage.npy', resizedSourceImage) '''
def collectCells(cell, patch, images, rho, alpha): id = cell[0] x = cell[1][0] y = cell[1][1] image = utils.getImage(id, images) C = [] c1 = image.cells[y - 1][x] c2 = image.cells[y + 1][x] c3 = image.cells[y][x - 1] c4 = image.cells[y][x + 1] if utils.identifyCell(c1, patch, rho): C.append(c1) if utils.identifyCell(c2, patch, rho): C.append(c2) if utils.identifyCell(c3, patch, rho): C.append(c3) if utils.identifyCell(c4, patch, rho): C.append(c4) return C
def get_image_with_border(self, data_path, image_path): """ returns the image with a border around the image Args: data_path: path to the csv file containing the crater coordinates (x,y) in the src image, the crater diameter and the label (1 = crater) in this order image_path: path to the src image Returns: image with border, border size """ src_img = utils.getImage(image_path) csvfile = open(data_path, 'rb') reader = csv.reader(csvfile, delimiter=',') # get max diameter max_diameter = 0 for row in reader: if int(row[2]) > max_diameter: max_diameter = int(row[2]) # add border border = max_diameter / 2 return cv2.copyMakeBorder(src_img, border, border, border, border, cv2.BORDER_REPLICATE), border
def _imgGen(self): while True: for index, row in self.DF.iterrows(): image, imgNum = getImage(self.array, row) yield row['cancer'], image
oImg.to_filename(filename) if __name__ == '__main__': INPUT_FOLDER = '/data/datasets/lung/sample_images/' DATADIR = '/data/datasets/lung/resampled_order1/' tsvFile = DATADIR + 'resampledImages.tsv' arrayFile = DATADIR + 'resampled.h5' DB = tables.open_file(arrayFile, mode='r') array = DB.root.resampled DF = pandas.read_csv(tsvFile, sep='\t') # DF = DF.sample(frac=1) # DF = DF[DF.cancer != -1] # remove images from the submission set DF = DF[DF.cancer != -1] # remove images from the submission set DF = DF[DF.cancer == 1] # remove images from the submission set DF = DF.head(1) row = DF.iloc[0] image, imgNum = getImage(array, row) image = zoom(image, 0.3) print image.shape print image.dtype vol2Nifti(image, 'firstcancer.nii.gz')
def main(_): data = loadData(args.dataDir, attend=(args.attend == 'True')) tf.reset_default_graph() graph = tf.Graph() with graph.as_default(): if args.attend: with tf.variable_scope(tf.get_variable_scope()): model = generate_attend_model() tf.get_variable_scope().reuse_variables() val_model = validate(max_len=20) else: if args.brnn: model = generate_brnn_model(mode=mode) else: model = generate_model(mode=mode) decayFunc = None learning_rate = tf.constant(Config.initial_learning_rate) if Config.learning_rate_decay_factor > 0: num_batches_per_epoch = \ (Config.num_examples_per_epoch / Config.batch_size) decay_steps = int(num_batches_per_epoch * Config.num_epochs_per_decay) def _decayFunc(learning_rate, global_step): return tf.train.exponential_decay( learning_rate, global_step, decay_steps=decay_steps, decay_rate=Config.learning_rate_decay_factor, staircase=True) decayFunc = _decayFunc op = tf.contrib.layers.optimize_loss( loss=model['total_loss'], global_step=model['global_step'], learning_rate=learning_rate, optimizer=Config.optimizer, clip_gradients=Config.clip_gradients, learning_rate_decay_fn=decayFunc) # initialize all variables init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) num_epochs = Config.total_num_epochs num_train = data['train_captions'].shape[0] iterations_per_epoch = max(num_train / Config.batch_size, 1) num_iterations = int(num_epochs * iterations_per_epoch) epoch = 0 loss_history = [] print("Total training iter: ", num_iterations) time_now = datetime.now() for t in range(num_iterations): total_loss_value = runModel(sess, data, op, model, Config.lstm_dropout_keep_prob, attend=(args.attend == 'True')) loss_history.append(total_loss_value) if t % 50 == 0: print('(Iteration %d / %d) loss: %f, and time "\ "elapsed: %.2f minutes' % (t + 1, num_iterations, float(loss_history[-1]), (datetime.now() - time_now).seconds / 60.0)) if (t + 1) % 5000 == 0: temp_dir = os.path.join(args.sample_dir, 'temp_dir_{}//'.format(t + 1)) if not os.path.exists(temp_dir): os.makedirs(temp_dir) captions_deco = None urls = None if args.attend: capt, gen_caps, urls = runValidation(sess, data, val_model, attend=True) captions_deco = decodeCaptions(gen_caps[0], data['idx_to_word']) else: captions_pred, urls = runValidation( sess, data, model, 1.0, brnn=(args.brnn == 'True')) captions_pred = [unpack.reshape(-1, 1) \ for unpack in captions_pred] captions_pred = np.concatenate(captions_pred, 1) captions_deco = decodeCaptions(captions_pred, data['idx_to_word']) for j in range(len(captions_deco)): img_name = os.path.join(temp_dir, 'image_{}.jpg'.format(j)) try: img = getImage(urls[j]) writeCaption(img, img_name, captions_deco[j]) except: continue if not os.path.exists(args.savedSession_dir): os.makedirs(args.savedSession_dir) save_path = model['saver'].save( sess, os.path.join(args.savedSession_dir, savedModelName)) print("done. Model saved at: ", os.path.join(args.savedSession_dir, savedModelName))
filters=filters) #cubes = DBo.create_carray(DBo.root, 'cubes', # atom=tables.Int16Atom(shape=CUBE_SHAPE), # shape=(len(DF),), filters=filters, chunkshape=(10,)) cubeDF = pandas.DataFrame() oldImgNum = None image = None L = [] for index, row in tqdm(DF.iterrows(), total=len(DF)): if row['imgNum'] != oldImgNum: #print ' new image loded ==============' del image image, imageNum = getImage(images, row, convertType=False) #print image.dtype if image.dtype != 'int16': image = image.astype('int16') oldImgNum = imageNum voxelC = row[['voxelZ', 'voxelY', 'voxelX']].as_matrix() cube = extractCubeAtLocation(image, voxelC, cubeSize) #print cube.shape #cubes.append([cube]) L.append(cube) #cubes[index] = cube row.name = index cubeDF = cubeDF.append(row)
def getImageFromCamera(): return utils.getImage(IMAGE_URL)
height_pos = int(math.ceil(float(positives_count) / 100) * FLAGS.image_size) height_neg = int(math.ceil(float(negatives_count) / 100) * FLAGS.image_size) img_pos = numpy.zeros((height_pos, 100 * FLAGS.image_size), numpy.float) img_neg = numpy.zeros((height_neg, 100 * FLAGS.image_size), numpy.float) y_pos = 0 y_neg = 0 x_pos = 0 x_neg = 0 # fill image for (image_path, label_path) in paths: # import data src = utils.getImage(image_path) src = cv2.copyMakeBorder(src, FLAGS.object_size, FLAGS.object_size, FLAGS.object_size, FLAGS.object_size, cv2.BORDER_REPLICATE) positives = get_labels(label_path, 1) negatives = get_labels(label_path, 0) height, width = src.shape for (x, y) in positives: # create positive example img = create_input_img(src, x + FLAGS.object_size, y + FLAGS.object_size) img_pos[y_pos : y_pos + FLAGS.image_size, x_pos : x_pos + FLAGS.image_size] = img x_pos += FLAGS.image_size if x_pos >= 100 * FLAGS.image_size: x_pos = 0 y_pos += FLAGS.image_size del img
def test_mlp_with_new_functionality(learning_rate=0.01, L1_reg=0.00, L2_reg=0.0001, n_epochs=100,batch_size=128, n_hidden=500, n_hiddenLayers=3,verbose=False, smaller_set=True,example_index = 0,adversarial_parameter = 0.01,distribution = 'constant'): """ Wrapper function for training and testing MLP :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient. :type L1_reg: float :param L1_reg: L1-norm's weight when added to the cost (see regularization). :type L2_reg: float :param L2_reg: L2-norm's weight when added to the cost (see regularization). :type n_epochs: int :param n_epochs: maximal number of epochs to run the optimizer. :type batch_size: int :param batch_szie: number of examples in minibatch. :type n_hidden: int or list of ints :param n_hidden: number of hidden units. If a list, it specifies the number of units in each hidden layers, and its length should equal to n_hiddenLayers. :type n_hiddenLayers: int :param n_hiddenLayers: number of hidden layers. :type verbose: boolean :param verbose: to print out epoch summary or not to. :type smaller_set: boolean :param smaller_set: to use the smaller dataset or not to. """ # load the dataset; download the dataset if it is not present train_set, valid_set, test_set = load_data(ds_rate=5, theano_shared=False) example_x = test_set[0][example_index:example_index+1] example_y = test_set[1][example_index:example_index+1] # example_x_reshape = numpy.reshape(example_x,(len(example_x),1)) # example_y_reshape = numpy.reshape(example_y,(1,1)) shared_example_x,shared_example_y = shared_dataset([example_x,example_y]) # shared_example_x = shared_example_x.reshape(shared_example_x.reshape[0],-1) # shared_example_x = theano.shared(type =theano.tensor.matrix,value = numpy.asarray(example_x,dtype=theano.config.floatX),borrow = True) # shared_example_y = theano.shared(type = theano.tensor.vector, value = numpy.asarray(example_y,dtype=theano.config.floatX),borrow = True) # Convert raw dataset to Theano shared variables. test_set_x, test_set_y = shared_dataset(test_set) valid_set_x, valid_set_y = shared_dataset(valid_set) train_set_x, train_set_y = shared_dataset(train_set) # compute number of minibatches for training, validation and testing n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] // batch_size n_test_batches = test_set_x.get_value(borrow=True).shape[0] // batch_size # print ' shapes of the shared_examples', shared_example_y,shared_example_x print 'n_train_batches : ',n_train_batches print 'n_valid_batches : ',n_valid_batches print 'n_test_batches : ',n_test_batches ###################### # BUILD ACTUAL MODEL # ###################### print('... building the model') # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch x = T.matrix('x') # the data is presented as rasterized images y = T.ivector('y') # the labels are presented as 1D vector of # [int] labels rng = numpy.random.RandomState(1234) # TODO: construct a neural network, either MLP or CNN. # input, n_in, n_hidden, n_out, n_hiddenLayers classifier = myMLP(rng, input = x, n_in =3072 , n_hidden = n_hidden, n_out= 10, n_hiddenLayers= n_hiddenLayers) # the cost we minimize during training is the negative log likelihood of # the model plus the regularization terms (L1 and L2); cost is expressed # here symbolically cost = ( classifier.negative_log_likelihood(y) + L1_reg * classifier.L1 + L2_reg * classifier.L2_sqr ) # compiling a Theano function that computes the mistakes that are made # by the model on a minibatch test_model = theano.function( inputs=[index], outputs=classifier.errors(y), givens={ x: test_set_x[index * batch_size:(index + 1) * batch_size], y: test_set_y[index * batch_size:(index + 1) * batch_size] } ) test_example = theano.function( inputs= [index], outputs=[classifier.errors(y),classifier.p_y_given_x], givens={ x: test_set_x[index * 1:(index + 1) * 1], y: test_set_y[index * 1:(index + 1) * 1] } ) test_example2 = theano.function( inputs= [], outputs=[classifier.errors(y),classifier.p_y_given_x], givens={ x: shared_example_x, y: shared_example_y } ) validate_model = theano.function( inputs=[index], outputs=classifier.errors(y), givens={ x: valid_set_x[index * batch_size:(index + 1) * batch_size], y: valid_set_y[index * batch_size:(index + 1) * batch_size] } ) # compute the gradient of cost with respect to theta (sotred in params) # the resulting gradients will be stored in a list gparams gparams = [T.grad(cost, param) for param in classifier.params] # specify how to update the parameters of the model as a list of # (variable, update expression) pairs # given two lists of the same length, A = [a1, a2, a3, a4] and # B = [b1, b2, b3, b4], zip generates a list C of same size, where each # element is a pair formed from the two lists : # C = [(a1, b1), (a2, b2), (a3, b3), (a4, b4)] updates = [ (param, param - learning_rate * gparam) for param, gparam in zip(classifier.params, gparams) ] # compiling a Theano function `train_model` that returns the cost, but # in the same time updates the parameter of the model based on the rules # defined in `updates` train_model = theano.function( inputs=[index], outputs=cost, updates=updates, givens={ x: train_set_x[index * batch_size: (index + 1) * batch_size], y: train_set_y[index * batch_size: (index + 1) * batch_size] } ) ############### # TRAIN MODEL # ############### print('... training') train_nn(train_model, validate_model, test_model, n_train_batches, n_valid_batches, n_test_batches, n_epochs, verbose) ################## # Performing adversarial example testing# ################## print '-------------------------------------' print 'example x :', example_x image = getImage(test_set[0][example_index]) plt.figure(1) plt.imshow(image) print 'example y :', example_y print '-------------------------------------' classification, probabilities = test_example(example_index) if int(classification) == 0: print 'Correct classification performed :', True else: print 'Correct classification performed :', False print 'probabilities : ',probabilities print 'Number predicted :', numpy.argmax(probabilities) print 'with probability :',numpy.max(probabilities)*100 print '-------------------------------------' gadversarial = T.vector() gadversarial = [T.grad(cost,x)] grad_cost_wrt_x = theano.function( inputs= [index], outputs=gadversarial, givens={ x: test_set_x[index * 1:(index + 1) * 1], y: test_set_y[index * 1:(index + 1) * 1] } ) print 'Creating adversarial example and trying to get results for that ... \n \n \n ' print '-------------------------------------' gradient_cost_wrt_x = grad_cost_wrt_x(example_index) gradient_sign = numpy.sign(gradient_cost_wrt_x) gradient_sign = numpy.reshape(gradient_sign,(1,3072)) adversarial_example_x = example_x + adversarial_parameter*gradient_sign input_image_x,input_image_y = shared_dataset([adversarial_example_x,example_y]) test_input_example = theano.function( inputs= [], outputs=[classifier.errors(y),classifier.p_y_given_x], givens={ x: input_image_x, y: input_image_y } ) adversarial_classification, input_image_output_probilities = test_input_example() if int(adversarial_classification) == 0: print 'Correct adversarial classification performed :', True else: print 'Correct adversarial classification performed :', False image2 = getImage(adversarial_example_x) plt.figure(2) plt.imshow(image2) print 'probabilities : ',input_image_output_probilities print 'Number predicted :', numpy.argmax(input_image_output_probilities) print 'with probability :',numpy.max(input_image_output_probilities)*100 print '-------------------------------------'
import numpy as np from network import Network from utils import getImage, squish # Reading image and initializing network file = 'data/training/0/img_1.jpg' image_data = getImage(file) squish_lambda = np.vectorize(squish) data = squish_lambda(image_data) shape = data.size, 16, 16, 10 network = Network(shape) network.create() output = network.run(data) print(output)
def getImageFromCamera(): return utils.getImage(IMAGE_URL) #utils.execute(CLOSED_FREQ, process)