def run_model(name, learning_rate, batch_size, epoch): #make model sess = tf.Session() md = models.Model(sess, name, FLAGS) #data preprocessing dp = utils.preprocess(FLAGS) x_data, y_data, x_test, y_test = dp.Mnist2data() #initialize sess.run(tf.global_variables_initializer()) #training for ep in range(epoch): avg_cost = 0 total_batches = int(x_data.shape[0]/batch_size) #print(int(x_data.shape[0]), total_batches, batch_size) for i in range(total_batches): x,y = x_data[i*batch_size:i*batch_size+batch_size], y_data[i*batch_size:i*batch_size+batch_size] cost, _ = md.train(x,y) avg_cost += cost/batch_size print('[*] epoch:', '%04d' % (ep + 1), ', cost =', '{:.9f}'.format(avg_cost)) print("[*] learning Finishied!") #testset predict print('Accuracy:', md.get_accuracy(x_test, y_test)) return True
def _load_from_disk(self, image_files, states, outputs): embnet_images, embnet_states, embnet_outputs = [], [], [] for i in range(self.support_query_size): images, emb_states, emb_outputs = self.data_sequencer.load( image_files[i], states[i], outputs[i]) # images will be of shape (sequence, w, h, 3) embnet_images.append(images) embnet_states.append(emb_states) embnet_outputs.append(emb_outputs) embed_images = tf.stack(embnet_images) embnet_states = tf.stack(embnet_states) embnet_outputs = tf.stack(embnet_outputs) embnet_states.set_shape( (self.support_query_size, self.data_sequencer.frames, None)) embnet_outputs.set_shape( (self.support_query_size, self.data_sequencer.frames, None)) # Grab a random timestep in one of the support and query trajectories ctrnet_timestep = tf.random_uniform( (2, ), 0, self.dataset.time_horizon, tf.int32) # The first should be a support and the last should be a query ctrnet_images = [ utils.tf_load_image(image_files[0], ctrnet_timestep[0]), utils.tf_load_image(image_files[-1], ctrnet_timestep[1]) ] ctrnet_states = [ states[0][ctrnet_timestep[0]], states[-1][ctrnet_timestep[1]] ] ctrnet_outputs = [ outputs[0][ctrnet_timestep[0]], outputs[-1][ctrnet_timestep[1]] ] ctrnet_images = tf.stack(ctrnet_images) ctrnet_states = tf.stack(ctrnet_states) ctrnet_outputs = tf.stack(ctrnet_outputs) embed_images = utils.preprocess(embed_images) ctrnet_images = utils.preprocess(ctrnet_images) return (embed_images, embnet_states, embnet_outputs, ctrnet_images, ctrnet_states, ctrnet_outputs)
def predict(self, imgs, sizes=None, visualize=False): # this function gives the prediction results of an image input if visualize: self.use_preset('visualize') prepared_imgs = list() sizes = list() for img in imgs: size = img.shape[1:] img = preprocess(tonumpy(img)) prepared_imgs.append(img) sizes.append(size) else: prepared_imgs = imgs bboxes = list() labels = list() scores = list() for img, size in zip(prepared_imgs, sizes): img = totensor(img[None]).float() scale = img.shape[3] / size[1] roi_cls_loc, roi_scores, rois, _ = self.forward(img, scale=scale) roi_score = roi_scores.data roi_cls_loc = roi_cls_loc.data roi = totensor(rois) / scale mean = t.Tensor(self.loc_normalize_mean).cuda(). \ repeat(self.n_class)[None] std = t.Tensor(self.loc_normalize_std).cuda(). \ repeat(self.n_class)[None] roi_cls_loc = (roi_cls_loc * std + mean) roi_cls_loc = roi_cls_loc.view(-1, self.n_class, 4) roi = roi.view(-1, 1, 4).expand_as(roi_cls_loc) cls_bbox = loc2bbox( tonumpy(roi).reshape((-1, 4)), tonumpy(roi_cls_loc).reshape((-1, 4))) cls_bbox = totensor(cls_bbox) cls_bbox = cls_bbox.view(-1, self.n_class * 4) # clip bounding box cls_bbox[:, 0::2] = (cls_bbox[:, 0::2]).clamp(min=0, max=size[0]) cls_bbox[:, 1::2] = (cls_bbox[:, 1::2]).clamp(min=0, max=size[1]) prob = (F.softmax(totensor(roi_score), dim=1)) bbox, label, score = self._suppress(cls_bbox, prob) bboxes.append(bbox) labels.append(label) scores.append(score) self.use_preset('evaluate') return bboxes, labels, scores
def predict(self, imgs, sizes=None, visualize=False): """Detect objects from images. This method predicts objects for each image. Args: imgs (iterable of numpy.ndarray): Arrays holding images. All images are in CHW and RGB format and the range of their value is :math:`[0, 255]`. sizes (iterable of ints) input image sizes. visualize: if use visusalize to show images result. Returns: tuple of lists: This method returns a tuple of three lists, :obj:`(bboxes, labels, scores)`. * **bboxes**: A list of float arrays of shape :math:`(R, 4)`, \ where :math:`R` is the number of bounding boxes in a image. \ Each bouding box is organized by \ :math:`(y_{min}, x_{min}, y_{max}, x_{max})` \ in the second axis. * **labels** : A list of integer arrays of shape :math:`(R,)`. \ Each value indicates the class of the bounding box. \ Values are in range :math:`[0, L - 1]`, where :math:`L` is the \ number of the foreground classes. * **scores** : A list of float arrays of shape :math:`(R,)`. \ Each value indicates how confident the prediction is. """ self.eval() # Visualize a few results getting from origin images. if visualize: self.use_present('visualize') prepared_imgs = list() sizes = list() for img in imgs: size = img.shape[1:] img = preprocess(img) img = torch.from_numpy(img) prepared_imgs.append(img) sizes.append(size) else: prepared_imgs = imgs bboxes = list() labels = list() scores = list() for img, size in zip(prepared_imgs, sizes): img = Variable(img[None].cuda(), volatile=True) scale = img.shape[3] / size[1] roi_cls_loc, roi_scores, rois, _ = self.forward(img, scale=scale) # We are assuming that batch size is 1 roi_score = roi_scores.data roi_cls_loc = roi_cls_loc.data roi = torch.from_numpy(rois).cuda() / scale # Convert predictions to bbox in image coordinates. # Bounding boxes are scaled to the scale of the input images. mean = torch.FloatTensor(self.loc_normalize_mean).cuda().repeat(self.n_class)[None] std = torch.FloatTensor(self.loc_normalize_std).cuda().repeat(self.n_class)[None] roi_cls_loc = (roi_cls_loc * std + mean) roi_cls_loc = roi_cls_loc.view(-1, self.n_class, 4) roi = roi.view(-1, 1, 4).expand_as(roi_cls_loc) cls_bbox = loc2bbox(roi.cpu().numpy().reshape((-1, 4)), roi_cls_loc.cpu().numpy().reshape((-1, 4))) cls_bbox = torch.from_numpy(cls_bbox).view(-1, self.n_class * 4) # Clip bbox. cls_bbox[:, 0::2] = (cls_bbox[:, 0::2]).clamp(min=0, max=size[0]) cls_bbox[:, 1::2] = (cls_bbox[:, 1::2]).clamp(min=0, max=size[1]) raw_cls_bbox = cls_bbox.numpy() raw_prob = F.softmax(Variable(roi_score), dim=1).cpu().data.numpy() bbox, label, score = self._supress(raw_cls_bbox, raw_prob) bboxes.append(bbox) labels.append(label) scores.append(score) self.use_present('evaluate') return bboxes, labels, scores
def consume(self, inputs): if self.disk_images: # (Examples,) input_image = tf.placeholder(tf.string, (self.support, )) else: # (Examples, timesteps) input_image = tf.placeholder(tf.float32, (None, None) + self.dataset.img_shape) input_states = tf.placeholder( tf.float32, (self.support, self.dataset.time_horizon, self.dataset.state_size)) input_outputs = tf.placeholder( tf.float32, (self.support, self.dataset.time_horizon, self.dataset.action_size)) # (B. W, H, C) input_ctr_image = tf.placeholder(tf.float32, (None, 1) + self.dataset.img_shape) input_ctr_state = tf.placeholder(tf.float32, (None, 1, self.dataset.state_size)) training = tf.placeholder_with_default(False, None) stacked_embnet_images, bs, cs = [], [], [] for i in range(self.support): embnet_images, embnet_states, embnet_outputs = ( self.data_sequencer.load(input_image[i], input_states[i], input_outputs[i])) embnet_images = utils.preprocess(embnet_images) stacked_embnet_images.append(embnet_images) bs.append(embnet_states) cs.append(embnet_outputs) embnet_images = tf.stack(stacked_embnet_images) embnet_images = tf.expand_dims(embnet_images, axis=0) # set batchsize 1 embnet_states = tf.stack(bs) embnet_states = tf.expand_dims(embnet_states, axis=0) embnet_outputs = tf.stack(cs) embnet_outputs = tf.expand_dims(embnet_outputs, axis=0) embnet_images.set_shape((None, None, self.data_sequencer.frames) + self.dataset.img_shape) embnet_states.set_shape( (None, None, self.data_sequencer.frames, self.dataset.state_size)) embnet_outputs.set_shape( (None, None, self.data_sequencer.frames, self.dataset.action_size)) return { 'embnet_images': embnet_images, 'embnet_states': embnet_states, 'embnet_outputs': embnet_outputs, 'input_image_files': input_image, 'input_states': input_states, 'input_outputs': input_outputs, 'ctrnet_images': input_ctr_image, 'ctrnet_states': input_ctr_state, 'training': training, 'support': tf.placeholder_with_default(self.support, None), 'query': tf.placeholder_with_default(0, None), }
def preproc_dataset(self): """Preprocesses dataset according to specified format (see utils)""" if (self.ds_name == 'wn18' or self.ds_name == 'fb15'): data_utils.preprocess(self.path)
import logging import time from config import CONFIG from data.utils import preprocess from gensim.models import KeyedVectors import gensim.downloader as api _LOGGER = logging.getLogger(__name__) _LOGGER.setLevel(logging.INFO) if __name__ == '__main__': WV_MODEL = api.load('glove-wiki-gigaword-300') _LOGGER.info("Starting data preprocessing") start = time.time() preprocess("{}/data/all.txt".format(CONFIG['base_dir']), "{}/data/cleaned.txt".format(CONFIG['base_dir']), WV_MODEL.vocab) end = time.time() _LOGGER.info("Took {:.2f} seconds to preprocess file".format(end-start))