def six_class(iter_idx): neural_net = ConvNet([ConvLayer(32, (128, 4), weight_scale=0.044, padding_mode=False), ActivationLayer('leakyReLU'), MaxPoolingLayerCUDA((1, 4)), ConvLayer(32, (32, 4), weight_scale=0.088, padding_mode=False), ActivationLayer('leakyReLU'), MaxPoolingLayerCUDA((1, 2)), ConvLayer(32, (32, 4), weight_scale=0.088, padding_mode=False), ActivationLayer('leakyReLU'), GlobalPoolingLayer(), FullyConnectedLayer(32, weight_scale=0.125), ActivationLayer('leakyReLU'), FullyConnectedLayer(32, weight_scale=0.125), ActivationLayer('leakyReLU'), FullyConnectedLayer(6, weight_scale=0.17), SoftmaxLayer()], DataProvider(num_genres=6)) time1 = time.time() neural_net.train(learning_rate=0.005, num_iters=80, lrate_schedule=True) time2 = time.time() print('Time taken: %.1fs' % (time2 - time1)) f = open('six_class_run' + str(iter_idx) + '.txt', 'w') f.write(str(neural_net.results)) f.close() print 'Iteration ' + str(iter_idx) print neural_net.results
def __init__(self, op, load_dic): ConvNet.__init__(self, op, load_dic) self.summary_dir = self.save_file + '_summary' if not os.path.exists(self.summary_dir): os.makedirs(self.summary_dir) ''' here, (train,test,pred) corresponds to (train,validation,testing)''' self.write_features = [self.write_features_train, self.write_features_test, self.write_features_pred]
def get_gpus(self): self.need_gpu = False if self.op.get_value('mode'): mode_value = self.op.get_value('mode') flag = mode_value in ['accveval'] self.need_gpu |= flag if self.need_gpu: ConvNet.get_gpus( self )
def init_model_lib(self): if self.need_gpu: if self.op.get_value('write_pixel_proj'): # in pixel projection model, activation matrix cannot be shared for l in self.model_state['layers']: l['usesActs'] = True ConvNet.init_model_lib(self)
def init_data_providers(self): class Dummy: def advance_batch(self): pass if self.need_gpu: ConvNet.init_data_providers(self) else: self.train_data_provider = self.test_data_provider = Dummy()
def init_data_providers(self): self.need_gpu = self.op.get_value('show_preds') class Dummy: def advance_batch(self): pass if self.need_gpu: ConvNet.init_data_providers(self) else: self.train_data_provider = self.test_data_provider = Dummy()
def get_gpus(self): self.need_gpu = ( self.op.get_value('show_preds') or self.op.get_value('write_features') or self.op.get_value('hist_features') or self.op.get_value('nn_analysis') or self.op.get_value('write_mv_result') or self.op.get_value('write_mv_mc_result') ) if self.need_gpu: ConvNet.get_gpus(self)
def init_data_providers(self): class Dummy: def advance_batch(self): pass if self.need_gpu: if self.dp_type == "imagenet": self.train_data_provider = ImageNetDataProvider(self.data_path, self.train_batch_range, test=False, show=True) self.test_data_provider = ImageNetDataProvider(self.data_path, self.test_batch_range, test=True, show=True) else: ConvNet.init_data_providers(self) else: self.train_data_provider = self.test_data_provider = Dummy()
def init_data_providers(self): class Dummy: def advance_batch(self): pass if self.need_gpu: ConvNet.init_data_providers(self) if self.op.get_value("write_features_pred") or self.op.get_value("show_preds") == 2: self.pred_data_provider = DataProvider.get_instance(self.libmodel, self.data_path, self.pred_batch_range, type=self.dp_type, dp_params=self.dp_params, test=DataProvider.DP_PREDICT) else: self.train_data_provider = self.test_data_provider = Dummy()
def __init__(self, model_path, data_processor, gpu, layers): op = ConvNetPredict.get_options_parser() op.set_value('load_file', model_path) op.set_value('gpu', str(gpu)) load_dic = IGPUModel.load_checkpoint(model_path) old_op = load_dic["op"] old_op.merge_from(op) op = old_op op.eval_expr_defaults() ConvNet.__init__(self, op, load_dic) self.dp = data_processor self.ftr_layer_idx = map(self.get_layer_idx, layers)
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('data_path_train', 'data_path_test', 'dp_type_train', 'dp_type_test', 'gpu', 'rnorm_const', 'img_provider_file', 'load_file', 'train_batch_range', 'test_batch_range', 'verbose'): op.delete_option(option) op.add_option("test-only", "test_only", BooleanOptionParser, "Test and quit?", default=1) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option("only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("write-features", "write_features", StringOptionParser, "Write test data features from given layer", default="", requires=['feature-path']) op.add_option("feature-path", "feature_path", StringOptionParser, "Write test data features to this path (to be used with --write-features)", default="") op.add_option("write-pixel-proj", "write_pixel_proj", StringOptionParser, "Write the projection of some response on pixel space", default = "", requires=['response_idx']) op.add_option("multiview", "mult_view", IntegerOptionParser, "Number of views for multi-view testing", default=1) op.add_option("scaleview", "scale_view", FloatOptionParser, "Scaling factor of the views in multi-view testing", default=1.0) op.add_option("bbxfile", "bbx_file", StringOptionParser, "Contains ground truth bounding box for each image", default="") op.add_option("imglist", "img_list", StringOptionParser, "Image list file", default="") op.add_option("clusterfile", "cluster_file", StringOptionParser, "Cluster center saved in pickle format", default="") op.options['load_file'].default = None return op
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('load_file', 'sigma'): op.delete_option(option) op.add_option("show-filters", "show_filters", StringOptionParser, "Show filters of specified layer", default="") op.add_option("norm-filters", "norm_filters", BooleanOptionParser, "Individually normalize filters shown with --show-filters", default=0) op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("smooth-test-errors", "smooth_test_errors", BooleanOptionParser, "Use running average for test error plot?", default=0) op.add_option("output-layer", "output_layer", StringOptionParser, "Output layer that the cost is computed from", default="") op.add_option("show-mse", "show_mse", BooleanOptionParser, "Show mse error (or PSNR error)", default=False) op.add_option("step", "step", IntegerOptionParser, "Step of x axis", default=1) op.add_option("patch-perbatch", "patch_perbatch", IntegerOptionParser, "Num of patches per batch", default=4096) op.add_option("result-path", "result_path", StringOptionParser, "Path to store the PSNR curve", default="") op.options['load_file'].default = None return op
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option("only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("write-features", "write_features", StringOptionParser, "Write test data features from given layer", default="", requires=['feature-path']) op.add_option("feature-path", "feature_path", StringOptionParser, "Write test data features to this path (to be used with --write-features)", default="") #----my options---------- op.add_option("hist-features", "hist_features", StringOptionParser, "plot histogram of feature activation", default="") op.add_option("hist-test", "hist_test", BooleanOptionParser, "True: plot hist of test data, False: plot hist of training data", default=True ) op.add_option("nn-analysis", "nn_analysis", StringOptionParser, "run inference on training mode for many times and analysis output", default="") #op.add_option("data-provider", "dp_type", StringOptionParser, "Data provider", default="default") #op.add_option("write-mv-result", "write_mv_result", StringOptionParser, "Write test data multiview features to file", default="", requires=['feature-path']) op.add_option("write-mv-result", "write_mv_result", StringOptionParser, "Write test data multiview features to file", default="" ) op.add_option("write-mv-mc-result", "write_mv_mc_result", StringOptionParser, "Write test data multiview features on MC inferenceto file", default="" ) op.options['load_file'].default = None return op
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range', 'multiview_test'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", ListOptionParser(IntegerOptionParser), "Cost function return value index for --show-cost", default=[]) # op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", IntegerOptionParser, "Show predictions made by given softmax on test or predicting set. 1:test set 2:predicting set", default=0) op.add_option("pred-batch-range", "pred_batch_range", RangeOptionParser, "Data batch range: predicting set") op.add_option("logsoftmax", "logsoftmax", StringOptionParser, "name of logsoftmax typed layer", default="") op.add_option("only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("write-features-train", "write_features_train", StringOptionParser, "Write train data features from given layer", default='', requires=[]) op.add_option("write-features-test", "write_features_test", StringOptionParser, "Write test data features from given layer", default='', requires=[]) op.add_option("write-features-pred", "write_features_pred", StringOptionParser, "Write prediction data features from given layer", default='', requires=[]) # op.add_option("feature-path", "feature_path", StringOptionParser, "Write test data features to this path (to be used with --write-features)", default="") op.add_option("show-layer-weights-biases", "show_layer_weights_biases", IntegerOptionParser, "Show evolution of layer-wise mean absolute of weights, weightsInc, bias, biasInc", default=0) op.add_option("confusion-mat", "confusion_mat", IntegerOptionParser, "plot confusion matrix", default=0) op.add_option("test-meta", "test_meta", StringOptionParser, "meta file for testing data used in plotting confusion matrix", default="") op.options['load_file'].default = None return op
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range', 'cam_test', 'test_one'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option("only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("write-features", "write_features", StringOptionParser, "Write test data features from given layer", default="", requires=['feature-path']) op.add_option("feature-path", "feature_path", StringOptionParser, "Write test data features to this path (to be used with --write-features)", default="") #add options here for streaming from the camera: send the results rather then save to file? op.add_option("show-preds-patch","show_preds_patch", StringOptionParser, "Show patch predictions made by given softmax on test set", default="") op.add_option("show-preds-patch-total","show_preds_patch_total", StringOptionParser, "Show patch predictions made by given softmax on test set in total image", default="") op.add_option("write-features-stream", "write_features_stream", StringOptionParser, "Stream test data features from given layer", default="", requires=['cam_test']) op.add_option("cam-test", "test_from_camera", BooleanOptionParser, "Get Test Batches from OpenNI Device?", default=0)#0? can i leave it False? #op.add_option("send-features", "send_features", StringOptionParser, "Send the test data features (probabilities) from given layer", default="", requires=['receiver-path']) #op.add_option("receiver-path", "receiver_path", StringOptionParser, "Send test data features to this (address?) (use with --send-features)", default="") #the above options should trigger an option in the net: test-one, and test-from-camera?: unlimited test batches, continuous until ctrl+c... op.options['load_file'].default = None return op
def init_model_state(self): ConvNet.init_model_state(self) if self.op.get_value('show_preds'): self.sotmax_idx = self.get_layer_idx(self.op.get_value('show_preds'), check_type='softmax') if self.op.get_value('write_features'): self.ftr_layer_idx = self.get_layer_idx(self.op.get_value('write_features')) if self.op.get_value('hist_features'): self.ftr_layer_idx = self.get_layer_idx(self.op.get_value('hist_features')) if self.op.get_value('nn_analysis'): self.ftr_layer_idx = self.get_layer_idx(self.op.get_value('nn_analysis')) if self.op.get_value('write_mv_result'): self.sotmax_idx = self.get_layer_idx('logprob', check_type='cost.logreg') if self.op.get_value('write_mv_mc_result'): self.sotmax_idx = self.get_layer_idx('logprob', check_type='cost.logreg')
def eight_class(): neural_net = ConvNet([ConvLayer(32, (128, 4), weight_scale=0.044, padding_mode=False), ActivationLayer('leakyReLU'), MaxPoolingLayerCUDA((1, 4)), ConvLayer(32, (32, 4), weight_scale=0.088, padding_mode=False), ActivationLayer('leakyReLU'), MaxPoolingLayerCUDA((1, 2)), ConvLayer(32, (32, 4), weight_scale=0.088, padding_mode=False), ActivationLayer('leakyReLU'), GlobalPoolingLayer(), FullyConnectedLayer(32, weight_scale=0.125), ActivationLayer('leakyReLU'), FullyConnectedLayer(32, weight_scale=0.125), ActivationLayer('leakyReLU'), FullyConnectedLayer(8, weight_scale=0.17), SoftmaxLayer()], DataProvider(num_genres=8)) neural_net.init_params_from_file(conv_only=True) time1 = time.time() neural_net.train(learning_rate=0.005, num_iters=80, lrate_schedule=True) time2 = time.time() print('Time taken: %.1fs' % (time2 - time1)) print "\nRESULTS:\n" for result in neural_net.results: print result for val in neural_net.results[result]: print val neural_net.serialise_params()
def init_model_state(self): ConvNet.init_model_state(self) if self.op.get_value('show_preds') or self.op.get_value('confusion_mat'): if not self.op.get_value('logsoftmax'): raise ShowNetError('logsoftmax typed layer is not specified') self.softmax_idx = self.get_layer_idx(self.op.get_value('logsoftmax'), check_type='logsoftmax') # self.softmax_idx = self.get_layer_idx(self.op.get_value('show_preds'), check_type='softmax') if self.op.get_value('confusion_mat') and not self.op.get_value('test_meta'): raise ShowNetError("test-meta is not specified") self.ftr_layer_idx = np.zeros((3), dtype=np.uint32) if self.op.get_value('write_features_train'): self.ftr_layer_idx[0] = self.get_layer_idx(self.op.get_value('write_features_train')) if self.op.get_value('write_features_test'): self.ftr_layer_idx[1] = self.get_layer_idx(self.op.get_value('write_features_test')) if self.op.get_value('write_features_pred'): self.ftr_layer_idx[2] = self.get_layer_idx(self.op.get_value('write_features_pred'))
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range', 'data_path', 'minibatch_size', 'layer_params', 'batch_size', 'test_only', 'test_one', 'shuffle_data', 'crop_one_border', 'external_meta_path'): op.delete_option(option) op.add_option('mode', 'mode', StringOptionParser, "The mode for evaluation") op.add_option('images-folder', 'images_folder', StringOptionParser, 'The folder for testing images') op.add_option('mean-image-path', 'mean_image_path', StringOptionParser, 'The path for mean image') op.options['load_file'].default = None return op
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range', 'data_path', 'minibatch_size', 'layer_params', 'batch_size', 'test_only', 'test_one', 'shuffle_data', 'crop_one_border'): op.delete_option(option) op.add_option('do-pose-evaluation', 'do_pose_evaluation', StringOptionParser, 'Specify the output layer of pose') op.add_option('inputstream', 'inputstream', StringOptionParser, 'Specify the type of camera to use [imgcamera|cvcamera]') op.add_option('outputdir', 'outputdir', StringOptionParser, 'Specify the directory for saving outputs') op.add_option('crop-image', 'crop_image', StringOptionParser, 'Specify the method to crop image to square input patch [faceubd]') op.options['load_file'].default = None return op
def __init__(self, feat_extract_name , n_processes, low_pass, high_pass, gauss_noise, roi, size_percentage,\ feature_extractor__shape_norm, feature_extractor__shape_conv, \ feature_extractor__shape_pool, feature_extractor__n_filters, \ feature_extractor__stride_pool, feature_extractor__stoc_pool, \ feature_extractor__div_norm, feature_extractor__region_shape, \ feature_extractor__region_stride, feature_extractor__top_regions, \ feature_extractor__stride_pool_recurrent, feature_extractor__analysis_shape, \ feature_extractor__method, \ feature_extractor__n_tiles, augmentation, multi_column, aug_rotate \ ): self.low_pass = low_pass self.high_pass = high_pass self.gauss_noise = gauss_noise self.roi = roi self.size_percentage = size_percentage self.augmentation = augmentation self.aug_rotate = aug_rotate self.multi_column = multi_column self.feat_extract_name = feat_extract_name self.n_processes = n_processes if feat_extract_name.lower() == 'convnet': self.feature_extractor = eval(feat_extract_name+'()') self.feature_extractor.n_filters = feature_extractor__n_filters self.feature_extractor.shape_norm = feature_extractor__shape_norm self.feature_extractor.shape_conv = feature_extractor__shape_conv self.feature_extractor.shape_pool = feature_extractor__shape_pool self.feature_extractor.stride_pool = feature_extractor__stride_pool self.feature_extractor.div_norm = feature_extractor__div_norm self.feature_extractor.stoc_pool = feature_extractor__stoc_pool elif feat_extract_name.lower() == 'mrrconvnet': self.feature_extractor = eval(feat_extract_name+'()') convnet = ConvNet() convnet.n_filters = feature_extractor__n_filters convnet.shape_norm = feature_extractor__shape_norm convnet.shape_conv = feature_extractor__shape_conv convnet.shape_pool = feature_extractor__shape_pool convnet.stride_pool = feature_extractor__stride_pool convnet.div_norm = feature_extractor__div_norm convnet.stoc_pool = feature_extractor__stoc_pool self.feature_extractor.convnet = convnet self.feature_extractor.region_shape = feature_extractor__region_shape self.feature_extractor.region_stride = feature_extractor__region_stride self.feature_extractor.top_regions = feature_extractor__top_regions self.feature_extractor.stride_pool_recurrent = feature_extractor__stride_pool_recurrent self.feature_extractor.analysis_shape =feature_extractor__analysis_shape elif feat_extract_name.lower() == 'lbp': self.feature_extractor = eval(feat_extract_name+'()') self.feature_extractor.method = feature_extractor__method self.feature_extractor.n_tiles = feature_extractor__n_tiles
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range', 'minibatch_size', 'data_path', 'dp_type'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters \ (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option("only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("write-features", "write_features", StringOptionParser, "Write test data features from given layer", default="", requires=['feature-path']) op.add_option("feature-path", "feature_path", StringOptionParser, "Write test data features to this path (to be used with --write-features)", default="") op.add_option("show-layers", "show_layers", StringOptionParser, "Show configurations of a layer", default="") op.add_option("output-one", "output_one", BooleanOptionParser, "Output to one file", default=False) op.add_option("feature-file", "feature_file", StringOptionParser, "Write test data features to this file (to be used with --output-one)", default="") op.add_option("write-predictions", "write_predictions", StringOptionParser, "Write predictions to a file", default="") # requires=['logreg_name'] op.add_option("multiview-test", "multiview_test", BooleanOptionParser, "Cropped DP: test on multiple patches?", default=False) op.options['load_file'].default = None return op
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option("only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("write-features", "write_features", StringOptionParser, "Write test data features from given layer", default="", requires=['feature-path']) op.add_option("feature-path", "feature_path", StringOptionParser, "Write test data features to this path (to be used with --write-features)", default="") op.add_option("save-to-file", "save_to_file", StringOptionParser, "Save the plot to file", default="") op.options['load_file'].default = None return op
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'inner_size', 'train_batch_range', 'test_batch_range', 'multiview_test', 'data_path', 'pca_noise', 'scalar_mean'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("norm-filters", "norm_filters", BooleanOptionParser, "Individually normalize filters shown with --show-filters", default=0) op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option("no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option("yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option("channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option("save-preds", "save_preds", StringOptionParser, "Save predictions to given path instead of showing them", default="") op.add_option("only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("local-plane", "local_plane", IntegerOptionParser, "Local plane to show", default=0) op.add_option("smooth-test-errors", "smooth_test_errors", BooleanOptionParser, "Use running average for test error plot?", default=1) op.options['load_file'].default = None return op
def __init__(self, env, params): self.env = env params.actions = env.actions() self.num_actions = env.actions() self.episodes = params.episodes self.steps = params.steps self.train_steps = params.train_steps self.update_freq = params.update_freq self.save_weights = params.save_weights self.history_length = params.history_length self.discount = params.discount self.eps = params.init_eps self.eps_delta = (params.init_eps - params.final_eps) / params.final_eps_frame self.replay_start_size = params.replay_start_size self.eps_endt = params.final_eps_frame self.random_starts = params.random_starts self.batch_size = params.batch_size self.ckpt_file = params.ckpt_dir + '/' + params.game self.global_step = tf.Variable(0, trainable=False) if params.lr_anneal: self.lr = tf.train.exponential_decay(params.lr, self.global_step, params.lr_anneal, 0.96, staircase=True) else: self.lr = params.lr self.buffer = Buffer(params) self.memory = Memory(params.size, self.batch_size) with tf.variable_scope("train") as self.train_scope: self.train_net = ConvNet(params, trainable=True) with tf.variable_scope("target") as self.target_scope: self.target_net = ConvNet(params, trainable=False) self.optimizer = tf.train.RMSPropOptimizer(self.lr, params.decay_rate, 0.0, self.eps) self.actions = tf.placeholder(tf.float32, [None, self.num_actions]) self.q_target = tf.placeholder(tf.float32, [None]) self.q_train = tf.reduce_max(tf.mul(self.train_net.y, self.actions), reduction_indices=1) self.diff = tf.sub(self.q_target, self.q_train) half = tf.constant(0.5) if params.clip_delta > 0: abs_diff = tf.abs(self.diff) clipped_diff = tf.clip_by_value(abs_diff, 0, 1) linear_part = abs_diff - clipped_diff quadratic_part = tf.square(clipped_diff) self.diff_square = tf.mul(half, tf.add(quadratic_part, linear_part)) else: self.diff_square = tf.mul(half, tf.square(self.diff)) if params.accumulator == 'sum': self.loss = tf.reduce_sum(self.diff_square) else: self.loss = tf.reduce_mean(self.diff_square) # backprop with RMS loss self.task = self.optimizer.minimize(self.loss, global_step=self.global_step)
def generator(input_shape, dimH, dimZ, dimY, n_channel, last_activation, name): # first construct p(y|z, x) # encoder for z (low res) layer_channels = [n_channel, n_channel * 2, n_channel * 4] filter_width = 5 filter_shapes = construct_filter_shapes(layer_channels, filter_width) fc_layer_sizes = [dimH] gen_conv, conv_output_shape = ConvNet(name+'_pyzx_conv', input_shape, filter_shapes, \ fc_layer_sizes, 'relu', last_activation = 'relu') print('generator shared Conv net ' + ' network architecture:', \ conv_output_shape, fc_layer_sizes) fc_layers = [dimZ + dimH, dimH, dimY] pyzx_mlp_layers = [] N_layers = len(fc_layers) - 1 l = 0 for i in range(N_layers): name_layer = name + '_pyzx_mlp_l%d' % l if i + 1 == N_layers: activation = 'linear' else: activation = 'relu' pyzx_mlp_layers.append( mlp_layer(fc_layers[i], fc_layers[i + 1], activation, name_layer)) def pyzx_params(z, x): fea = gen_conv(x) out = tf.concat([fea, z], axis=1) for layer in pyzx_mlp_layers: out = layer(out) return out # now construct p(z|x) # encoder for z (low res) layer_channels = [n_channel, n_channel * 2, n_channel * 4] filter_width = 5 filter_shapes = construct_filter_shapes(layer_channels, filter_width) fc_layer_sizes = [dimH] gen_conv2, conv_output_shape = ConvNet(name+'_pzx_conv', input_shape, filter_shapes, \ fc_layer_sizes, 'relu', last_activation = 'relu') print('generator shared Conv net ' + ' network architecture:', \ conv_output_shape, fc_layer_sizes) fc_layers = [dimH, dimH, dimZ * 2] pzx_mlp_layers = [] N_layers = len(fc_layers) - 1 l = 0 for i in range(N_layers): name_layer = name + '_pzx_mlp_l%d' % l if i + 1 == N_layers: activation = 'linear' else: activation = 'relu' pzx_mlp_layers.append( mlp_layer(fc_layers[i], fc_layers[i + 1], activation, name_layer)) def pzx_params(x): out = gen_conv2(x) for layer in pzx_mlp_layers: out = layer(out) mu, log_sig = tf.split(out, 2, axis=1) return mu, log_sig return pyzx_params, pzx_params
def import_model(self): if self.need_gpu: ConvNet.import_model(self) print 'finish_2'
def get_gpus(self): self.need_gpu = self.op.get_value('show_preds') \ or self.op.get_value('write_features') \ or self.op.get_value('write_predictions') if self.need_gpu: ConvNet.get_gpus(self)
def __init__(self, op, load_dic): ConvNet.__init__(self, op, load_dic)
def import_model(self): if self.need_gpu: ConvNet.import_model(self)
def init_model_lib(self): if self.need_gpu: ConvNet.init_model_lib(self)
return dropout, reg cifar10 = cifar10_utils.get_cifar10('./cifar10/cifar-10-batches-py') print('done getting cifar10') image_shape = cifar10.train.images.shape[1:4] num_classes = cifar10.test.labels.shape[1] # Construct linear convnet graph x = tf.placeholder(tf.float32, shape=[None] + list(image_shape), name='x') y = tf.placeholder(tf.int32, shape=(None, num_classes), name='y') is_training = tf.placeholder(dtype=tf.bool, shape=(), name='isTraining') model = ConvNet(is_training=is_training) _ = model.inference(x) fc2 = model.fc2 #%% train_size_lm = 2000 test_size = 1000 ckpt_file_in = 'epoch14000.ckpt' #subdir = './' subdir = 'checkpoints_new/' dropouts = [0., 0.1, 0.2, 0.3, 0.4, 0.5] regs = ['%.e' % i for i in [0., 0.1, 0.01, 0.001, 0.0001]] epochs = np.arange(10000, 15000, 1000) cols = [i for i in product(epochs, dropouts, regs)]
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # Try the code with some different datasets. Currently, MNIST and CIFAR10 # are supported, but you could easily add more in dataloader.py trainloader, testloader = dataloader.get_dataloader(dataset, batch_size) # We only handle square images for now. The image shape can be read from # the shape of the elements in the dataloader. The shape of each batch # in the dataloader is on the format: # (num_images, num_channels, width, height) # num_images is capped at batch_size. num_channels represent the number of # colors channels in the image: usually 1 (gray-scale) or 3 (colored). # Since we expect the width and height to be the same, we read the third # shape value (width) as our image size first_batch = next(iter(trainloader))[0] img_size = first_batch.shape[2] num_colors = first_batch.shape[1] net = ConvNet(code_size, img_size, num_colors, device).to(device) optimizer = optim.Adam(net.parameters(), lr=1e-3, weight_decay=1e-5) t_start = time.time() t_last = time.time() for epoch in range(epochs): train(trainloader, device) test(testloader, device) print( f'{time.time()-t_start:.1f}s\t{time.time()-t_last:.1f}s\tDone running epoch {epoch+1}' ) t_last = time.time() # Save the learned weights for later use torch.save(net, f'static/{dataset_name}_state.pth') # We want to sample some values sent to the decoder. The reason is that # we want to use this to define a range for each of the n nodes in the # code that we can use in the front end. We do this by sending a batch
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'inner_size', 'train_batch_range', 'test_batch_range', 'multiview_test', 'data_path', 'pca_noise', 'scalar_mean', 'em_feature_path'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option( "norm-filters", "norm_filters", BooleanOptionParser, "Individually normalize filters shown with --show-filters", default=0) op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option( "no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option( "yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option( "channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option("save-preds", "save_preds", StringOptionParser, "Save predictions to given path instead of showing them", default="") op.add_option( "only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("local-plane", "local_plane", IntegerOptionParser, "Local plane to show", default=0) op.add_option("smooth-test-errors", "smooth_test_errors", BooleanOptionParser, "Use running average for test error plot?", default=1) op.options['load_file'].default = None return op
def get_gpus(self): self.need_gpu = self.op.get_value('show_preds') or self.op.get_value('write_features') if self.need_gpu: ConvNet.get_gpus(self) print 'finish_0'
logs = [] colors = [] trace_names = [] if args.plot_name is not None: visdom_live_plot = PlotLearning( './plots/cifar/', 10, plot_name=args.plot_name, env_name=args.env_name) else: visdom_live_plot = None start_time = time.time() print("\n\n > Teacher (Base Network) training ... ") net_type = 'Teacher' colors.append('orange') trace_names.extend(['Teacher Train', 'Teacher Test']) teacher_model = ConvNet(net_dataset=CIFAR10) teacher_model.cuda() optimizer = get_optimizer(teacher_model) scheduler = get_scheduler(optimizer) print teacher_model log_base, win_accuracy, win_loss = start_training( teacher_model, net_type, optimizer, scheduler, visdom_live_plot) logs.append(log_base) save_optimizer_scheduler(optimizer, scheduler, net_type) end_time = time.time() print 'time to train teacher network:', print end_time-start_time # wider student training from Net2Net print("\n\n > Wider Student training (Net2Net)... ") net_type = 'WideNet2Net'
def init_model_lib(self): if self.need_gpu: ConvNet.init_model_lib(self) print 'finish_4'
def get_options_parser(cls): op = ConvNet.get_options_parser() for option in list(op.options): if option not in ('gpu', 'load_file', 'train_batch_range', 'test_batch_range'): op.delete_option(option) op.add_option("show-cost", "show_cost", StringOptionParser, "Show specified objective function", default="") op.add_option("show-filters", "show_filters", StringOptionParser, "Show learned filters in specified layer", default="") op.add_option("input-idx", "input_idx", IntegerOptionParser, "Input index for layer given to --show-filters", default=0) op.add_option("cost-idx", "cost_idx", IntegerOptionParser, "Cost function return value index for --show-cost", default=0) op.add_option( "no-rgb", "no_rgb", BooleanOptionParser, "Don't combine filter channels into RGB in layer given to --show-filters", default=False) op.add_option( "yuv-to-rgb", "yuv_to_rgb", BooleanOptionParser, "Convert RGB filters to YUV in layer given to --show-filters", default=False) op.add_option( "channels", "channels", IntegerOptionParser, "Number of channels in layer given to --show-filters (fully-connected layers only)", default=0) op.add_option("show-preds", "show_preds", StringOptionParser, "Show predictions made by given softmax on test set", default="") op.add_option( "only-errors", "only_errors", BooleanOptionParser, "Show only mistaken predictions (to be used with --show-preds)", default=False, requires=['show_preds']) op.add_option("write-features", "write_features", StringOptionParser, "Write test data features from given layer", default="", requires=['feature-path']) op.add_option( "feature-path", "feature_path", StringOptionParser, "Write test data features to this path (to be used with --write-features)", default="") op.add_option("write-predictions", "write_predictions", StringOptionParser, "Write predictions to a file", default="") op.add_option("multiview-test", "multiview_test", BooleanOptionParser, "Cropped DP: test on multiple patches?", default=0) # requires=['logreg_name'] op.options['load_file'].default = None return op