def init_testnet(test_net, trained_model=None, test_device=0): caffe.set_mode_gpu() caffe.select_device(test_device, False) if (trained_model == None): return caffe.Net(test_net, caffe.TEST) else: return caffe.Net(test_net, trained_model, caffe.TEST)
def run_test(self, iteration): caffe.select_device(self.options.test_device, False) for dataset_i in range(len(self.datasets)): dataset_to_process = self.datasets[dataset_i] if 'name' in dataset_to_process: h5_file_name = dataset_to_process['name'] + '_iter_' + str(iteration) + '.h5' else: h5_file_name = 'test_out_' + repr(dataset_i) + '_iter_' + str(iteration) + '.h5' temp_file_name = h5_file_name + '.inprogress' # fix from Larissa H with h5py.File(temp_file_name, 'w') as h5_file: output_array = [] process(self.test_net, [dataset_to_process], self.output_blob_names, output_array, self.callback) for blob_name in self.output_blob_names: prediction_shape = self.output_specs[blob_name].shape print('prediction_shape of blob', blob_name, prediction_shape) out = output_array[0][blob_name] print(out.shape) h5_file.create_dataset(name=blob_name, shape=out.shape, dtype=np.float32, data=out) os.rename(temp_file_name, h5_file_name) print("Just saved {}".format(h5_file_name))
def init_testnet(test_net, trained_model=None, test_device=0): caffe.set_mode_gpu() caffe.select_device(test_device, False) if(trained_model == None): return caffe.Net(test_net, caffe.TEST) else: return caffe.Net(test_net, trained_model, caffe.TEST)
def run_generate_training(self): global data_slices, label_slices, data_offsets # TODO: Add this option to caffe options and use that, rather than 4 arbitrarily # caffe.select_device(self.options.augment_device, False) caffe.select_device(4, False) # print "Shapes of data and label arrays: ", self.data_arrays[0].shape, self.label_arrays[0].shape # print "Data and label sizes: ", self.data_sizes, self.label_sizes training_it = 0 while True: if data_slices.qsize() > 5: continue print "Generating Training Point #{0}".format(training_it) training_it += 1 dataset = randint(0, len(data_arrays) - 1) data_slice, label_slice, data_offset = getSampleVolume( self.data_arrays[dataset], self.label_arrays[dataset], self.input_padding, self.data_sizes, self.label_sizes) data_slices.put(data_slice) label_slices.put(label_slice) data_offsets.put(data_offset)
def init_solver(solver_config, options): caffe.set_mode_gpu() caffe.select_device(options.train_device, False) solver_inst = caffe.get_solver(solver_config) if options.test_net is None: return solver_inst, None else: return solver_inst, init_testnet(options.test_net, test_device=options.test_device, level=options.test_level, stages=options.test_stages)
def init_solver(solver_config, options): caffe.set_mode_gpu() caffe.select_device(options.train_device, False) solver_inst = caffe.get_solver(solver_config) if (options.test_net == None): return (solver_inst, None) else: return (solver_inst, init_testnet(options.test_net, test_device=options.test_device))
def run_test(self, iteration): caffe.select_device(self.options.test_device, False) pred_arrays = process(self.test_net, self.data_arrays, shapes=self.shapes, net_io=self.net_io) for i in range(0, len(pred_arrays)): h5file = 'test_out_' + repr(i) + '.h5' outhdf5 = h5py.File(h5file, 'w') outdset = outhdf5.create_dataset('main', pred_arrays[i].shape, np.float32, data=pred_arrays[i]) # outdset.attrs['nhood'] = np.string_('-1,0,0;0,-1,0;0,0,-1') outhdf5.close()
def init_testnet(test_net, trained_model=None, test_device=0): print('--->init_testnet') caffe.set_mode_gpu() print('--->selecting test device...', test_device) caffe.select_device(test_device, False) print('--->going to create nets...') if (trained_model == None): print('--->creating test net...') return caffe.Net(test_net, caffe.TEST) else: print('--->creating test and train net...') return caffe.Net(test_net, trained_model, caffe.TEST)
def run_test(self, iteration): caffe.select_device(self.options.test_device, False) pred_arrays = process(self.test_net, self.data_arrays, shapes=self.shapes, net_io=self.net_io) for i in range(0, len(pred_arrays)): if ('name' in self.data_arrays[i]): h5file = self.data_arrays[i]['name'] + '.h5' else: h5file = 'test_out_' + repr(i) + '.h5' outhdf5 = h5py.File(h5file, 'w') outdset = outhdf5.create_dataset('main', pred_arrays[i].shape, np.float32, data=pred_arrays[i]) # outdset.attrs['nhood'] = np.string_('-1,0,0;0,-1,0;0,0,-1') outhdf5.close()
def process_core_multithreaded(device_locks, net_io, data_slices, dataset_indexes, offsets, output_arrays): # Each thread sets its GPU current_device_id = -1 while (current_device_id == -1): for device_list_id in range(0,len(device_locks)): if (device_locks[device_list_id].acquire(False)): current_device_id = device_list_id break if current_device_id == -1: time.sleep(0.0005) if config.debug: print("Using device (list ID): ", current_device_id) # Note that this is the list ID, not the absolute device ID caffe.select_device(current_device_id, True) process_core(net_io[current_device_id], data_slices, dataset_indexes, offsets, output_arrays) device_locks[device_list_id].release()
def run_test(self, iteration): caffe.select_device(self.options.test_device, False) for dataset_i in range(len(self.datasets)): dataset_to_process = self.datasets[dataset_i] if 'name' in dataset_to_process: h5_file_name = dataset_to_process['name'] + '.h5' else: h5_file_name = 'test_out_' + repr(dataset_i) + '.h5' temp_file_name = h5_file_name + '.inprogress' with h5py.File(temp_file_name, 'w') as h5_file: prediction_shape = (self.fmaps_out,) + dataset_to_process['data'].shape[-self.n_data_dims:] target_array = h5_file.create_dataset(name='main', shape=prediction_shape, dtype=np.float32) output_arrays = process(self.test_net, data_arrays=[dataset_to_process], shapes=self.shapes, net_io=self.net_io, target_arrays=[target_array]) os.rename(temp_file_name, h5_file_name) print("Just saved {}".format(h5_file_name))
def init_testnet(test_net, trained_model=None, test_device=0, level=0, stages=None): caffe.set_mode_gpu() if isinstance(test_device, list): # Initialize test network for each device networks = [] for device in test_device: caffe.select_device(device, False) if trained_model is None: networks += [caffe.Net(test_net, caffe.TEST, level=level, stages=stages)] else: networks += [caffe.Net(test_net, trained_model, caffe.TEST, level=level, stages=stages)] return networks else: # Initialize test network for a single device caffe.select_device(test_device, False) if trained_model is None: return caffe.Net(test_net, caffe.TEST, level=level, stages=stages) else: return caffe.Net(test_net, trained_model, caffe.TEST, level=level, stages=stages)
def process_core_multithreaded(device_locks, net_io, data_slice, offsets, pred_array, input_padding, fmaps_out, output_dims, using_data_loader, offsets_to_enqueue, processing_data_loader, index_of_shared_dataset, source_dataset_index): # Each thread sets its GPU current_device_id = -1 while (current_device_id == -1): for device_list_id in range(0,len(device_locks)): if (device_locks[device_list_id].acquire(False)): current_device_id = device_list_id break if current_device_id == -1: time.sleep(0.0005) if DEBUG: print("Using device (list ID): ", current_device_id) # Note that this is the list ID, not the absolute device ID caffe.select_device(current_device_id, True) process_core(net_io[current_device_id], data_slice, offsets, pred_array, input_padding, fmaps_out, output_dims, using_data_loader, offsets_to_enqueue, processing_data_loader, index_of_shared_dataset, source_dataset_index) device_locks[device_list_id].release()
def run_test(self, iteration): caffe.select_device(self.options.test_device, False) self.pred_arrays = process(self.test_net, self.data_arrays, shapes=self.shapes, net_io=self.net_io) for i in range(0, 1): #for i in range(0, len(self.data_arrays)): if ('name' in self.data_arrays[i]): h5file = self.data_arrays[i]['name'] + '.h5' else: h5file = 'test_out_' + repr(i) + '.h5' outhdf5 = h5py.File(h5file, 'w') outdset = outhdf5.create_dataset('main', self.pred_arrays[i * 2].shape, np.float32, data=self.pred_arrays[i * 2]) # outdset.attrs['nhood'] = np.string_('-1,0,0;0,-1,0;0,0,-1') outhdf5.close() count = 0 #pdb.set_trace() self.pred_arrays_samesize = [] for i in range(0, len(self.pred_arrays), 2): pred_array1 = self.pred_arrays[i] pred_mask = self.pred_arrays[i + 1] nz_idx = np.where(pred_mask[0, ...] > 0) pred_array1_samesize = np.zeros(pred_mask.shape).astype(np.float32) for cc in range(pred_array1_samesize.shape[0]): pred_array1_samesize[cc, nz_idx[0], nz_idx[1], nz_idx[2]] = pred_array1[cc, ...].ravel() self.pred_arrays_samesize.append([]) self.pred_arrays_samesize[-1] = pred_array1_samesize
def run_generate_training(self): global data_slices, label_slices, data_offsets # TODO: Add this option to caffe options and use that, rather than 4 arbitrarily # caffe.select_device(self.options.augment_device, False) caffe.select_device(4, False) # print "Shapes of data and label arrays: ", self.data_arrays[0].shape, self.label_arrays[0].shape # print "Data and label sizes: ", self.data_sizes, self.label_sizes training_it = 0 while True: if data_slices.qsize() > 5: continue print "Generating Training Point #{0}".format(training_it) training_it += 1 dataset = randint(0, len(data_arrays)-1) data_slice, label_slice, data_offset = getSampleVolume(self.data_arrays[dataset], self.label_arrays[dataset], self.input_padding, self.data_sizes, self.label_sizes) data_slices.put(data_slice) label_slices.put(label_slice) data_offsets.put(data_offset)
def train(solver, test_net, data_arrays, train_data_arrays, options): global data_slices, label_slices, data_offsets caffe.select_device(options.train_device, False) net = solver.net test_eval = None if (options.test_net != None): test_eval = TestNetEvaluator(test_net, net, train_data_arrays, options) input_dims, output_dims, input_padding = get_spatial_io_dims(net) fmaps_in, fmaps_out = get_fmap_io_dims(net) dims = len(output_dims) losses = [] shapes = [] # Raw data slice input (n = 1, f = 1, spatial dims) shapes += [[1, fmaps_in] + input_dims] # Label data slice input (n = 1, f = #edges, spatial dims) shapes += [[1, fmaps_out] + output_dims] if (options.loss_function == 'malis'): # Connected components input (n = 1, f = 1, spatial dims) shapes += [[1, 1] + output_dims] if (options.loss_function == 'euclid'): # Error scale input (n = 1, f = #edges, spatial dims) shapes += [[1, fmaps_out] + output_dims] # Nhood specifications (n = #edges, f = 3) if (('nhood' in data_arrays[0]) and (options.loss_function == 'malis')): shapes += [[1, 1] + list(np.shape(data_arrays[0]['nhood']))] net_io = NetInputWrapper(net, shapes) data_sizes = [fmaps_in] + [ output_dims[di] + input_padding[di] for di in range(0, dims) ] label_sizes = [fmaps_out] + output_dims # Begin the generation of the training set training_set = TrainingSetGenerator(data_arrays, options, data_sizes, label_sizes, input_padding) training_set.generate_training() # Loop from current iteration to last iteration for i in range(solver.iter, solver.max_iter): if (options.test_net != None and i % options.test_interval == 0): test_eval.evaluate(i) # First pick the dataset to train with dataset = randint(0, len(data_arrays) - 1) offsets = [] for j in range(0, dims): offsets.append( randint( 0, data_arrays[dataset]['data'].shape[1 + j] - (output_dims[j] + input_padding[j]))) # These are the raw data elements data_slice_old = slice_data(data_arrays[dataset]['data'], [0] + offsets, data_sizes) data_slice = data_slices.get() # print "Compare sizes of data_slices: {0} and {1}".format(data_slice_old.shape, data_slice.shape) label_slice = None components_slice = None if (options.training_method == 'affinity'): if ('label' in data_arrays[dataset]): label_slice_old = slice_data( data_arrays[dataset]['label'], [0] + [ offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims) ], label_sizes) label_slice = label_slices.get() # print "Compare sizes of label_slices: {0} and {1}".format(label_slice_old.shape, label_slice.shape) # TODO: Not sure about what to do for components_slice if ('components' in data_arrays[dataset]): data_offset = data_offsets.get() components_slice = slice_data( data_arrays[dataset]['components'][0, :], [ data_offset[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims) ], output_dims) if (label_slice is None or options.recompute_affinity): label_slice = malis.seg_to_affgraph( components_slice, data_arrays[dataset]['nhood']).astype(float32) if (components_slice is None or options.recompute_affinity): components_slice, ccSizes = malis.connected_components_affgraph( label_slice.astype(int32), data_arrays[dataset]['nhood']) else: label_slice_old = slice_data(data_arrays[dataset]['label'], [0] + [ offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims) ], [fmaps_out] + output_dims) label_slice = label_slices.get() if options.loss_function == 'malis': # Also recomputing the corresponding labels (connected components) net_io.setInputs([ data_slice, label_slice, components_slice, data_arrays[0]['nhood'] ]) if options.loss_function == 'euclid': if (options.scale_error == True): frac_pos = np.clip(label_slice.mean(), 0.05, 0.95) w_pos = 1.0 / (2.0 * frac_pos) w_neg = 1.0 / (2.0 * (1.0 - frac_pos)) else: w_pos = 1 w_neg = 1 net_io.setInputs([ data_slice, label_slice, error_scale(label_slice, w_neg, w_pos) ]) if options.loss_function == 'softmax': # These are the affinity edge values net_io.setInputs([data_slice, label_slice]) # Single step loss = solver.step(1) # sanity_check_net_blobs(net) while gc.collect(): pass if options.loss_function == 'euclid' or options.loss_function == 'euclid_aniso': print("[Iter %i] Loss: %f, frac_pos=%f, w_pos=%f" % (i, loss, frac_pos, w_pos)) else: print("[Iter %i] Loss: %f" % (i, loss)) # TODO: Store losses to file losses += [loss]
def train(solver, test_net, data_arrays, train_data_arrays, options): caffe.select_device(options.train_device, False) net = solver.net test_eval = None if (options.test_net != None): test_eval = TestNetEvaluator(test_net, net, train_data_arrays, options) input_dims, output_dims, input_padding = get_spatial_io_dims(net) fmaps_in, fmaps_out = get_fmap_io_dims(net) dims = len(output_dims) losses = [] shapes = [] # Raw data slice input (n = 1, f = 1, spatial dims) shapes += [[1,fmaps_in] + input_dims] # Label data slice input (n = 1, f = #edges, spatial dims) shapes += [[1,fmaps_out] + output_dims] if (options.loss_function == 'malis'): # Connected components input (n = 1, f = 1, spatial dims) shapes += [[1,1] + output_dims] if (options.loss_function == 'euclid'): # Error scale input (n = 1, f = #edges, spatial dims) shapes += [[1,fmaps_out] + output_dims] # Nhood specifications (n = #edges, f = 3) if (('nhood' in data_arrays[0]) and (options.loss_function == 'malis')): shapes += [[1,1] + list(np.shape(data_arrays[0]['nhood']))] net_io = NetInputWrapper(net, shapes) # Loop from current iteration to last iteration for i in range(solver.iter, solver.max_iter): if (options.test_net != None and i % options.test_interval == 0): test_eval.evaluate(i) # First pick the dataset to train with dataset = randint(0, len(data_arrays) - 1) offsets = [] for j in range(0, dims): offsets.append(randint(0, data_arrays[dataset]['data'].shape[1+j] - (output_dims[j] + input_padding[j]))) # These are the raw data elements data_slice = slice_data(data_arrays[dataset]['data'], [0]+offsets, [fmaps_in]+[output_dims[di] + input_padding[di] for di in range(0, dims)]) label_slice = slice_data(data_arrays[dataset]['label'], [0] + [offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims)], [fmaps_out] + output_dims) # transform the input # this code assumes that the original input pixel values are scaled between (0,1) if 'transform' in data_arrays[dataset]: # print('Pre:',(data_slice.min(),data_slice.mean(),data_slice.max())) lo, hi = data_arrays[dataset]['transform']['scale'] data_slice = 0.5 + (data_slice-0.5)*np.random.uniform(low=lo,high=hi) lo, hi = data_arrays[dataset]['transform']['shift'] data_slice = data_slice + np.random.uniform(low=lo,high=hi) # print('Post:',(data_slice.min(),data_slice.mean(),data_slice.max())) if options.loss_function == 'malis': components_slice,ccSizes = malis.connected_components_affgraph(label_slice.astype(int32), data_arrays[dataset]['nhood']) # Also recomputing the corresponding labels (connected components) net_io.setInputs([data_slice, label_slice, components_slice, data_arrays[0]['nhood']]) if options.loss_function == 'euclid': if(options.scale_error == True): frac_pos = np.clip(label_slice.mean(),0.05,0.95) w_pos = 1.0/(2.0*frac_pos) w_neg = 1.0/(2.0*(1.0-frac_pos)) else: w_pos = 1 w_neg = 1 net_io.setInputs([data_slice, label_slice, error_scale(label_slice,w_neg,w_pos)]) if options.loss_function == 'softmax': # These are the affinity edge values net_io.setInputs([data_slice, label_slice]) # Single step loss = solver.step(1) # sanity_check_net_blobs(net) while gc.collect(): pass if options.loss_function == 'euclid' or options.loss_function == 'euclid_aniso': print("[Iter %i] Loss: %f, frac_pos=%f, w_pos=%f" % (i,loss,frac_pos,w_pos)) else: print("[Iter %i] Loss: %f" % (i,loss)) # TODO: Store losses to file losses += [loss] if hasattr(options, 'loss_snapshot') and ((i % options.loss_snapshot) == 0): io.savemat('loss.mat',{'loss':losses})
def train(solver, options, train_data_arrays, data_slice_callback, test_net, test_data_arrays, test_data_slice_callback, data_offsets={}, scales={}, test_data_offsets={}, test_scales={}, test_output_blob_names={}, eval=None): caffe.select_device(options.train_device, False) net = solver.net test_eval = None if eval: test_eval = eval elif (options.test_net != None): test_eval = TestNetEvaluator( test_net, net, test_data_arrays, options, test_data_slice_callback, output_blob_names=test_output_blob_names) # Get the networks input specifications input_specs = get_net_input_specs(net, data_offsets=data_offsets, scales=scales) max_shape = [] if (len(train_data_arrays) > 0): dataset_for_keys = train_data_arrays[0] for set_key in input_specs.keys(): if (input_specs[set_key].name in dataset_for_keys.keys()): shape = input_specs[set_key].scaled_shape() for j in range(0, len(shape)): if len(max_shape) <= j: max_shape.append(shape[j]) else: max_shape[j] = max(max_shape[j], shape[j]) for set_key in input_specs.keys(): if (input_specs[set_key].name in train_data_arrays[0].keys()): input_specs[set_key].compute_spatial_offsets(max_shape) batch_size = max_shape[0] net_io = NetInputWrapper(net, input_specs=input_specs) offset_generator = OffsetGenerator(True, net_input_specs=net_io.input_specs) # Loop from current iteration to last iteration for i in range(solver.iter, solver.max_iter): start = time.time() if (options.test_net != None and i % options.test_interval == 1 and i > 10 ): test_eval.evaluate(i) if config.use_one_thread: # after testing finishes, switch back to the training device caffe.select_device(options.train_device, False) dataset_indexes, offsets, dataset_combined_sizes = offset_generator.make_dataset_offsets(batch_size, train_data_arrays, max_shape=max_shape) slices = {} if (len(train_data_arrays) > 0): dataset_for_keys = train_data_arrays[0] for set_key in dataset_for_keys.keys(): data_slice = input_specs[set_key].slice_data(batch_size, dataset_indexes, offsets, dataset_combined_sizes, train_data_arrays) slices[set_key] = data_slice data_slice_callback(input_specs, batch_size, dataset_indexes, offsets, dataset_combined_sizes, train_data_arrays, slices) net_io.set_inputs(slices) loss = solver.step(1) # Single step while gc.collect(): pass time_of_iteration = time.time() - start
def train(solver, test_net, data_arrays, train_data_arrays, options, random_blacks=False): caffe.select_device(options.train_device, False) net = solver.net #pdb.set_trace() clwt = None test_eval = None if options.scale_error == 2: clwt = ClassWeight(data_arrays, solver.iter) test_eval = TestNetEvaluator(test_net, net, data_arrays, options) test_eval2 = None if (options.test_net != None): test_eval2 = TestNetEvaluator(test_net, net, train_data_arrays, options) input_dims, output_dims, input_padding = get_spatial_io_dims(net) fmaps_in, fmaps_out = get_fmap_io_dims(net) dims = len(output_dims) losses = [] shapes = [] # Raw data slice input (n = 1, f = 1, spatial dims) shapes += [[1, fmaps_in] + input_dims] # Label data slice input (n = 1, f = #edges, spatial dims) shapes += [[1, fmaps_out] + output_dims] if (options.loss_function == 'malis'): # Connected components input (n = 1, f = 1, spatial dims) shapes += [[1, 1] + output_dims] if (options.loss_function == 'euclid'): # Error scale input (n = 1, f = #edges, spatial dims) shapes += [[1, fmaps_out] + output_dims] # Nhood specifications (n = #edges, f = 3) if (('nhood' in data_arrays[0]) and (options.loss_function == 'malis')): shapes += [[1, 1] + list(np.shape(data_arrays[0]['nhood']))] net_io = NetInputWrapper(net, shapes) weight_vec = [] if (options.loss_function == 'softmax' or options.loss_function == 'euclid') and options.scale_error == 1: #pdb.set_trace() weight_vec = class_balance_distribution(data_arrays[0]['label']) #weight_vec[2] = weight_vec[1]*4.0 #for 3 class, inversed during weighting #pdb.set_trace() # Loop from current iteration to last iteration for i in range(solver.iter, solver.max_iter): if (options.test_net != None and i % options.test_interval == 0 and i > 1): #pdb.set_trace() test_eval2.evaluate(i) if options.scale_error == 2: test_eval.evaluate(i) clwt.recompute_weight(test_eval.pred_arrays_samesize, i) # First pick the dataset to train with dataset = randint(0, len(data_arrays) - 1) if dims == 3: offsets = [] for j in range(0, dims): offsets.append( randint( 0, data_arrays[dataset]['data'].shape[1 + j] - (output_dims[j] + input_padding[j]))) # These are the raw data elements #pdb.set_trace() data_slice = slice_data( data_arrays[dataset]['data'], [0] + offsets, [fmaps_in] + [output_dims[di] + input_padding[di] for di in range(0, dims)]) label_slice = slice_data(data_arrays[dataset]['label'], [0] + [ offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims) ], [fmaps_out] + output_dims) if options.scale_error == 2 and clwt != None: weight_slice = slice_data(clwt.class_weights[dataset], [0] + [ offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims) ], [fmaps_out] + output_dims) elif dims == 2: offsets = [] offsets.append( randint(0, data_arrays[dataset]['data'].shape[1] - 1)) for j in range(0, dims): offsets.append( randint( 0, data_arrays[dataset]['data'].shape[2 + j] - (output_dims[j] + input_padding[j]))) # These are the raw data elements #pdb.set_trace() data_slice = slice_data( data_arrays[dataset]['data'], [0] + offsets, [fmaps_in, 1] + [output_dims[di] + input_padding[di] for di in range(0, dims)]) label_slice = slice_data( data_arrays[dataset]['label'], [0, offsets[0]] + [ offsets[di + 1] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims) ], [fmaps_out, 1] + output_dims) data_slice = np.squeeze(data_slice) label_slice = np.squeeze(label_slice) #offsets=np.zeros(dims); if (data_slice.shape[0] < 1) or (label_slice.shape[0] < 2): pp = 1 pdb.set_trace() #pdb.set_trace() #if(np.unique(label_slice).shape[0]<2): # continue; # transform the input # this code assumes that the original input pixel values are scaled between (0,1) if 'transform' in data_arrays[dataset]: # print('Pre:',(data_slice.min(),data_slice.mean(),data_slice.max())) data_slice_mean = data_slice.mean() lo, hi = data_arrays[dataset]['transform']['scale'] data_slice = data_slice_mean + ( data_slice - data_slice_mean) * np.random.uniform(low=lo, high=hi) lo, hi = data_arrays[dataset]['transform']['shift'] data_slice = data_slice + np.random.uniform(low=lo, high=hi) # print('Post:',(data_slice.min(),data_slice.mean(),data_slice.max())) data_slice = np.clip(data_slice, 0.0, 0.95) #pdb.set_trace() if random_blacks and np.random.random() < 0.25: data_slice[randint(0, data_slice.shape[0] - 1), ...] = 0.0 if options.loss_function == 'malis': components_slice, ccSizes = malis.connected_components_affgraph( label_slice.astype(int32), data_arrays[dataset]['nhood']) # Also recomputing the corresponding labels (connected components) net_io.setInputs([ data_slice, label_slice, components_slice, data_arrays[0]['nhood'] ]) if options.loss_function == 'euclid': ###if(options.scale_error == True): ###frac_pos = np.clip(label_slice.mean(),0.05,0.95) #for binary labels ###w_pos = 1.0/(2.0*frac_pos) ###w_neg = 1.0/(2.0*(1.0-frac_pos)) ###else: ###w_pos = 1 ###w_neg = 1 ###net_io.setInputs([data_slice, label_slice, error_scale(label_slice,w_neg,w_pos)]) if (options.scale_error == 3): frac_pos = np.clip(label_slice.mean(), 0.01, 0.99) #for binary labels w_pos = 1.0 / (2.0 * frac_pos) w_neg = 1.0 / (2.0 * (1.0 - frac_pos)) net_io.setInputs([ data_slice, label_slice, error_scale(label_slice, w_neg, w_pos) ]) elif (options.scale_error == 1): frac_pos = weight_vec[0] w_pos = 1. / frac_pos label_weights = error_scale_overall(label_slice, weight_vec) net_io.setInputs([data_slice, label_slice, label_weights]) elif options.scale_error == 2: net_io.setInputs([data_slice, label_slice, weight_slice]) elif options.scale_error == 0: net_io.setInputs([data_slice, label_slice]) if options.loss_function == 'softmax': net_io.setInputs([data_slice, label_slice]) # Single step loss = solver.step(1) # sanity_check_net_blobs(net) while gc.collect(): pass if (options.loss_function == 'euclid' or options.loss_function == 'euclid_aniso') and options.scale_error == 1: print("[Iter %i] Loss: %f, frac_pos=%f, w_pos=%f" % (i, loss, frac_pos, w_pos)) else: print("[Iter %i] Loss: %f" % (i, loss)) # TODO: Store losses to file losses += [loss] if hasattr(options, 'loss_snapshot') and ((i % options.loss_snapshot) == 0): io.savemat('loss.mat', {'loss': losses})
def main(argv): pycaffe_dir = os.path.dirname(__file__) parser = argparse.ArgumentParser() # Required arguments: input and output files. parser.add_argument("input_file", help="Input image, directory, or npy.") parser.add_argument("output_file", help="Output npy filename.") # Optional arguments. parser.add_argument( "--model_def", default=os.path.join( pycaffe_dir, "../models/bvlc_reference_caffenet/deploy.prototxt"), help="Model definition file.") parser.add_argument( "--pretrained_model", default=os.path.join( pycaffe_dir, "../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel" ), help="Trained model weights file.") parser.add_argument("--gpu", action='store_true', help="Switch for gpu computation.") parser.add_argument( "--center_only", action='store_true', help="Switch for prediction from center crop alone instead of " + "averaging predictions across crops (default).") parser.add_argument( "--images_dim", default='256,256', help="Canonical 'height,width' dimensions of input images.") parser.add_argument( "--mean_file", default=os.path.join(pycaffe_dir, 'caffe/imagenet/ilsvrc_2012_mean.npy'), help="Data set image mean of [Channels x Height x Width] dimensions " + "(numpy array). Set to '' for no mean subtraction.") parser.add_argument( "--input_scale", type=float, help="Multiply input features by this scale to finish preprocessing.") parser.add_argument( "--raw_scale", type=float, default=255.0, help="Multiply raw input by this scale before preprocessing.") parser.add_argument( "--channel_swap", default='2,1,0', help="Order to permute input channels. The default converts " + "RGB -> BGR since BGR is the Caffe default by way of OpenCV.") parser.add_argument( "--ext", default='jpg', help="Image file extension to take as input when a directory " + "is given as the input file.") args = parser.parse_args() image_dims = [int(s) for s in args.images_dim.split(',')] mean, channel_swap = None, None if args.mean_file: mean = np.load(args.mean_file) if args.channel_swap: channel_swap = [int(s) for s in args.channel_swap.split(',')] if args.gpu: caffe.set_mode_gpu() caffe.set_devices((0, )) caffe.select_device(0, True) print("GPU mode") else: caffe.set_mode_cpu() print("CPU mode") # Make classifier. classifier = caffe.Classifier(args.model_def, args.pretrained_model, image_dims=image_dims, mean=mean, input_scale=args.input_scale, raw_scale=args.raw_scale, channel_swap=channel_swap) # Load numpy array (.npy), directory glob (*.jpg), or image file. args.input_file = os.path.expanduser(args.input_file) if args.input_file.endswith('npy'): print("Loading file: %s" % args.input_file) inputs = np.load(args.input_file) elif os.path.isdir(args.input_file): print("Loading folder: %s" % args.input_file) inputs = [ caffe.io.load_image(im_f) for im_f in glob.glob(args.input_file + '/*.' + args.ext) ] else: print("Loading file: %s" % args.input_file) inputs = [caffe.io.load_image(args.input_file)] print("Classifying %d inputs." % len(inputs)) # Classify. start = time.time() predictions = classifier.predict(inputs, not args.center_only) print("Done in %.2f s." % (time.time() - start)) # Save print("Saving results into %s" % args.output_file) np.save(args.output_file, predictions)
def train(solver, test_net, data_arrays, train_data_arrays, options): global data_slices, label_slices, data_offsets caffe.select_device(options.train_device, False) net = solver.net test_eval = None if (options.test_net != None): test_eval = TestNetEvaluator(test_net, net, train_data_arrays, options) input_dims, output_dims, input_padding = get_spatial_io_dims(net) fmaps_in, fmaps_out = get_fmap_io_dims(net) dims = len(output_dims) losses = [] shapes = [] # Raw data slice input (n = 1, f = 1, spatial dims) shapes += [[1,fmaps_in] + input_dims] # Label data slice input (n = 1, f = #edges, spatial dims) shapes += [[1,fmaps_out] + output_dims] if (options.loss_function == 'malis'): # Connected components input (n = 1, f = 1, spatial dims) shapes += [[1,1] + output_dims] if (options.loss_function == 'euclid'): # Error scale input (n = 1, f = #edges, spatial dims) shapes += [[1,fmaps_out] + output_dims] # Nhood specifications (n = #edges, f = 3) if (('nhood' in data_arrays[0]) and (options.loss_function == 'malis')): shapes += [[1,1] + list(np.shape(data_arrays[0]['nhood']))] net_io = NetInputWrapper(net, shapes) data_sizes = [fmaps_in]+[output_dims[di] + input_padding[di] for di in range(0, dims)] label_sizes = [fmaps_out] + output_dims # Begin the generation of the training set training_set = TrainingSetGenerator(data_arrays, options, data_sizes, label_sizes, input_padding) training_set.generate_training() # Loop from current iteration to last iteration for i in range(solver.iter, solver.max_iter): if (options.test_net != None and i % options.test_interval == 0): test_eval.evaluate(i) # First pick the dataset to train with dataset = randint(0, len(data_arrays) - 1) offsets = [] for j in range(0, dims): offsets.append(randint(0, data_arrays[dataset]['data'].shape[1+j] - (output_dims[j] + input_padding[j]))) # These are the raw data elements data_slice_old = slice_data(data_arrays[dataset]['data'], [0]+offsets, data_sizes) data_slice = data_slices.get() # print "Compare sizes of data_slices: {0} and {1}".format(data_slice_old.shape, data_slice.shape) label_slice = None components_slice = None if (options.training_method == 'affinity'): if ('label' in data_arrays[dataset]): label_slice_old = slice_data(data_arrays[dataset]['label'], [0] + [offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims)], label_sizes) label_slice = label_slices.get() # print "Compare sizes of label_slices: {0} and {1}".format(label_slice_old.shape, label_slice.shape) # TODO: Not sure about what to do for components_slice if ('components' in data_arrays[dataset]): data_offset = data_offsets.get() components_slice = slice_data(data_arrays[dataset]['components'][0,:], [data_offset[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims)], output_dims) if (label_slice is None or options.recompute_affinity): label_slice = malis.seg_to_affgraph(components_slice, data_arrays[dataset]['nhood']).astype(float32) if (components_slice is None or options.recompute_affinity): components_slice,ccSizes = malis.connected_components_affgraph(label_slice.astype(int32), data_arrays[dataset]['nhood']) else: label_slice_old = slice_data(data_arrays[dataset]['label'], [0] + [offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims)], [fmaps_out] + output_dims) label_slice = label_slices.get() if options.loss_function == 'malis': # Also recomputing the corresponding labels (connected components) net_io.setInputs([data_slice, label_slice, components_slice, data_arrays[0]['nhood']]) if options.loss_function == 'euclid': if(options.scale_error == True): frac_pos = np.clip(label_slice.mean(),0.05,0.95) w_pos = 1.0/(2.0*frac_pos) w_neg = 1.0/(2.0*(1.0-frac_pos)) else: w_pos = 1 w_neg = 1 net_io.setInputs([data_slice, label_slice, error_scale(label_slice,w_neg,w_pos)]) if options.loss_function == 'softmax': # These are the affinity edge values net_io.setInputs([data_slice, label_slice]) # Single step loss = solver.step(1) # sanity_check_net_blobs(net) while gc.collect(): pass if options.loss_function == 'euclid' or options.loss_function == 'euclid_aniso': print("[Iter %i] Loss: %f, frac_pos=%f, w_pos=%f" % (i,loss,frac_pos,w_pos)) else: print("[Iter %i] Loss: %f" % (i,loss)) # TODO: Store losses to file losses += [loss]
def main(argv): pycaffe_dir = os.path.dirname(__file__) parser = argparse.ArgumentParser() # Required arguments: input and output. parser.add_argument( "input_file", help="Input txt/csv filename. If .txt, must be list of filenames.\ If .csv, must be comma-separated file with header\ 'filename, xmin, ymin, xmax, ymax'") parser.add_argument( "output_file", help="Output h5/csv filename. Format depends on extension.") # Optional arguments. parser.add_argument( "--model_def", default=os.path.join( pycaffe_dir, "../models/bvlc_reference_caffenet/deploy.prototxt"), help="Model definition file.") parser.add_argument( "--pretrained_model", default=os.path.join( pycaffe_dir, "../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel" ), help="Trained model weights file.") parser.add_argument("--crop_mode", default="selective_search", choices=CROP_MODES, help="How to generate windows for detection.") parser.add_argument("--gpu", action='store_true', help="Switch for gpu computation.") parser.add_argument( "--mean_file", default=os.path.join(pycaffe_dir, 'caffe/imagenet/ilsvrc_2012_mean.npy'), help="Data set image mean of H x W x K dimensions (numpy array). " + "Set to '' for no mean subtraction.") parser.add_argument( "--input_scale", type=float, help="Multiply input features by this scale to finish preprocessing.") parser.add_argument( "--raw_scale", type=float, default=255.0, help="Multiply raw input by this scale before preprocessing.") parser.add_argument( "--channel_swap", default='2,1,0', help="Order to permute input channels. The default converts " + "RGB -> BGR since BGR is the Caffe default by way of OpenCV.") parser.add_argument( "--context_pad", type=int, default='16', help="Amount of surrounding context to collect in input window.") args = parser.parse_args() mean, channel_swap = None, None if args.mean_file: mean = np.load(args.mean_file) if mean.shape[1:] != (1, 1): mean = mean.mean(1).mean(1) if args.channel_swap: channel_swap = [int(s) for s in args.channel_swap.split(',')] if args.gpu: caffe.set_mode_gpu() caffe.set_devices((0, )) caffe.select_device(0, True) print("GPU mode") else: caffe.set_mode_cpu() print("CPU mode") # Make detector. detector = caffe.Detector(args.model_def, args.pretrained_model, mean=mean, input_scale=args.input_scale, raw_scale=args.raw_scale, channel_swap=channel_swap, context_pad=args.context_pad) # Load input. t = time.time() print("Loading input...") if args.input_file.lower().endswith('txt'): with open(args.input_file) as f: inputs = [_.strip() for _ in f.readlines()] elif args.input_file.lower().endswith('csv'): inputs = pd.read_csv(args.input_file, sep=',', dtype={'filename': str}) inputs.set_index('filename', inplace=True) else: raise Exception("Unknown input file type: not in txt or csv.") # Detect. if args.crop_mode == 'list': # Unpack sequence of (image filename, windows). images_windows = [ (ix, inputs.iloc[np.where(inputs.index == ix)][COORD_COLS].values) for ix in inputs.index.unique() ] detections = detector.detect_windows(images_windows) else: detections = detector.detect_selective_search(inputs) print("Processed {} windows in {:.3f} s.".format(len(detections), time.time() - t)) # Collect into dataframe with labeled fields. df = pd.DataFrame(detections) df.set_index('filename', inplace=True) df[COORD_COLS] = pd.DataFrame(data=np.vstack(df['window']), index=df.index, columns=COORD_COLS) del (df['window']) # Save results. t = time.time() if args.output_file.lower().endswith('csv'): # csv # Enumerate the class probabilities. class_cols = ['class{}'.format(x) for x in range(NUM_OUTPUT)] df[class_cols] = pd.DataFrame(data=np.vstack(df['feat']), index=df.index, columns=class_cols) df.to_csv(args.output_file, cols=COORD_COLS + class_cols) else: # h5 df.to_hdf(args.output_file, 'df', mode='w') print("Saved to {} in {:.3f} s.".format(args.output_file, time.time() - t))
def train(solver, test_net, data_arrays, train_data_arrays, options): caffe.select_device(options.train_device, False) net = solver.net test_eval = None if (options.test_net != None): test_eval = TestNetEvaluator(test_net, net, train_data_arrays, options) input_dims, output_dims, input_padding = get_spatial_io_dims(net) fmaps_in, fmaps_out = get_fmap_io_dims(net) dims = len(output_dims) losses = [] shapes = [] # Raw data slice input (n = 1, f = 1, spatial dims) shapes += [[1,fmaps_in] + input_dims] # Label data slice input (n = 1, f = #edges, spatial dims) shapes += [[1,fmaps_out] + output_dims] if (options.loss_function == 'malis'): # Connected components input (n = 1, f = 1, spatial dims) shapes += [[1,1] + output_dims] if (options.loss_function == 'euclid'): # Error scale input (n = 1, f = #edges, spatial dims) shapes += [[1,fmaps_out] + output_dims] # Nhood specifications (n = #edges, f = 3) if (('nhood' in data_arrays[0]) and (options.loss_function == 'malis')): shapes += [[1,1] + list(np.shape(data_arrays[0]['nhood']))] net_io = NetInputWrapper(net, shapes) make_dataset_offset = MakeDatasetOffset(input_dims, output_dims) if data_io.data_loader_should_be_used_with(data_arrays): using_data_loader = True # and initialize queue! loader_size = 20 n_workers = 10 make_dataset_offset = MakeDatasetOffset(dims, output_dims, input_padding) loader_kwargs = dict( size=loader_size, datasets=data_arrays, input_shape=tuple(input_dims), output_shape=tuple(output_dims), n_workers=n_workers, dataset_offset_func=make_dataset_offset ) print("creating queue with kwargs {}".format(loader_kwargs)) training_data_loader = data_io.DataLoader(**loader_kwargs) # start populating the queue for i in range(loader_size): if DEBUG: print("Pre-populating data loader's dataset #{i}/{size}" .format(i=i, size=training_data_loader.size)) shared_dataset_index, async_result = \ training_data_loader.start_refreshing_shared_dataset(i) else: using_data_loader = False # Loop from current iteration to last iteration for i in range(solver.iter, solver.max_iter): start = time.time() if (options.test_net != None and i % options.test_interval == 1): test_eval.evaluate(i) if USE_ONE_THREAD: # after testing finishes, switch back to the training device caffe.select_device(options.train_device, False) if not using_data_loader: dataset_index, offsets = make_dataset_offset(data_arrays) dataset = data_arrays[dataset_index] # These are the raw data elements data_slice = data_io.util.get_zero_padded_slice_from_array_by_offset( array=dataset['data'], origin=[0] + offsets, shape=[fmaps_in] + input_dims) label_slice = slice_data(dataset['label'], [0] + [offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims)], [fmaps_out] + output_dims) if 'transform' in dataset: # transform the input # assumes that the original input pixel values are scaled between (0,1) if DEBUG: print("data_slice stats, pre-transform: min", data_slice.min(), "mean", data_slice.mean(), "max", data_slice.max()) lo, hi = dataset['transform']['scale'] data_slice = 0.5 + (data_slice - 0.5) * np.random.uniform(low=lo, high=hi) lo, hi = dataset['transform']['shift'] data_slice = data_slice + np.random.uniform(low=lo, high=hi) else: dataset, index_of_shared_dataset = training_data_loader.get_dataset() data_slice = dataset['data'] assert data_slice.shape == (fmaps_in,) + tuple(input_dims) label_slice = dataset['label'] assert label_slice.shape == (fmaps_out,) + tuple(output_dims) if DEBUG: print("Training with next dataset in data loader, which has offset", dataset['offset']) mask_slice = None if 'mask' in dataset: mask_slice = dataset['mask'] if DEBUG: print("data_slice stats: min", data_slice.min(), "mean", data_slice.mean(), "max", data_slice.max()) if options.loss_function == 'malis': components_slice, ccSizes = malis.connected_components_affgraph(label_slice.astype(int32), dataset['nhood']) # Also recomputing the corresponding labels (connected components) net_io.setInputs([data_slice, label_slice, components_slice, data_arrays[0]['nhood']]) elif options.loss_function == 'euclid': label_slice_mean = label_slice.mean() if 'mask' in dataset: label_slice = label_slice * mask_slice label_slice_mean = label_slice.mean() / mask_slice.mean() w_pos = 1.0 w_neg = 1.0 if options.scale_error: frac_pos = np.clip(label_slice_mean, 0.05, 0.95) w_pos = w_pos / (2.0 * frac_pos) w_neg = w_neg / (2.0 * (1.0 - frac_pos)) error_scale_slice = scale_errors(label_slice, w_neg, w_pos) net_io.setInputs([data_slice, label_slice, error_scale_slice]) elif options.loss_function == 'softmax': # These are the affinity edge values net_io.setInputs([data_slice, label_slice]) loss = solver.step(1) # Single step if using_data_loader: training_data_loader.start_refreshing_shared_dataset(index_of_shared_dataset) while gc.collect(): pass time_of_iteration = time.time() - start if options.loss_function == 'euclid' or options.loss_function == 'euclid_aniso': print("[Iter %i] Time: %05.2fs Loss: %f, frac_pos=%f, w_pos=%f" % (i, time_of_iteration, loss, frac_pos, w_pos)) else: print("[Iter %i] Time: %05.2fs Loss: %f" % (i, time_of_iteration, loss)) losses += [loss] if hasattr(options, 'loss_snapshot') and ((i % options.loss_snapshot) == 0): io.savemat('loss.mat',{'loss':losses}) if using_data_loader: training_data_loader.destroy()
def train(solver, test_net, data_arrays, train_data_arrays, options): caffe.select_device(options.train_device, False) print('====> in training....') net = solver.net net.debug_info = True #pdb.set_trace() ''' clwt=None test_eval = None if options.scale_error == 2: clwt = ClassWeight(data_arrays, solver.iter) test_eval = TestNetEvaluator(test_net, net, data_arrays, options) test_eval2 = None if (options.test_net != None): test_eval2 = TestNetEvaluator(test_net, net, train_data_arrays, options) ''' input_dims, output_dims, input_padding = get_spatial_io_dims(net) fmaps_in, fmaps_out = get_fmap_io_dims(net) print('input_dims:', input_dims) print('output_dims:', output_dims) print('input_padding:', input_padding) print('fmaps_out:', fmaps_out) dims = len(output_dims) losses = [] shapes = [] # Raw data slice input (n = 1, f = 1, spatial dims) shapes += [[1, fmaps_in] + input_dims] # Label data slice input (n = 1, f = #edges, spatial dims) shapes += [[1, fmaps_out] + output_dims] if (options.loss_function == 'malis'): # Connected components input (n = 1, f = 1, spatial dims) shapes += [[1, 1] + output_dims] if (options.loss_function == 'euclid'): # Error scale input (n = 1, f = #edges, spatial dims) shapes += [[1, fmaps_out] + output_dims] # Nhood specifications (n = #edges, f = 3) if (('nhood' in data_arrays[0]) and (options.loss_function == 'malis')): shapes += [[1, 1] + list(np.shape(data_arrays[0]['nhood']))] net_io = NetInputWrapper(net, shapes) weight_vec = [] if (options.loss_function == 'softmax' or options.loss_function == 'euclid') and options.scale_error == 1: #pdb.set_trace() weight_vec = class_balance_distribution(data_arrays[0]['label']) #weight_vec[2] = weight_vec[1]*4.0 #for 3 class, inversed during weighting #pdb.set_trace() ''' dims3d = dims + 1 output_dims3d = output_dims + [output_dims[-1]] input_padding3d = input_padding + [input_padding[-1]] output_dims3d = output_dims + [output_dims[-1]] ''' n_slices = output_dims[-1] i_slice = 0 # Loop from current iteration to last iteration for i in range(solver.iter, solver.max_iter): ''' if (options.test_net != None and i % options.test_interval == 0 and i>1): #pdb.set_trace() test_eval2.evaluate(i) if options.scale_error == 2: test_eval.evaluate(i) clwt.recompute_weight(test_eval.pred_arrays_samesize, i) ''' # First pick the dataset to train with dataset = randint(0, len(data_arrays) - 1) #print('dataset shape:', data_arrays[dataset]['data'].shape) if i_slice == 0 or i_slice == n_slices: i_slice = 0 offsets = [] for j in range(0, dims): offsets.append( randint( 0, data_arrays[dataset]['data'].shape[1 + j] - (output_dims[j] + input_padding[j]))) data_slice = slice_data( data_arrays[dataset]['data'], [0] + offsets, [fmaps_in] + [output_dims[di] + input_padding[di] for di in range(0, dims)]) label_slice = slice_data(data_arrays[dataset]['label'], [0] + [ offsets[di] + int(math.ceil(input_padding[di] / float(2))) for di in range(0, dims) ], [fmaps_out] + output_dims) #print(data_slice.shape) #print(label_slice.shape) #data_slice = np.squeeze(data_slice) label_slice = np.squeeze(label_slice) #print(label_slice) #offsets=np.zeros(dims); if (data_slice.shape[0] < 1) or (label_slice.shape[0] < 2): pp = 1 # transform the input # this code assumes that the original input pixel values are scaled between (0,1) if 'transform' in data_arrays[dataset]: # print('Pre:',(data_slice.min(),data_slice.mean(),data_slice.max())) data_slice_mean = data_slice.mean() lo, hi = data_arrays[dataset]['transform']['scale'] data_slice = data_slice_mean + ( data_slice - data_slice_mean) * np.random.uniform(low=lo, high=hi) lo, hi = data_arrays[dataset]['transform']['shift'] data_slice = data_slice + np.random.uniform(low=lo, high=hi) # print('Post:',(data_slice.min(),data_slice.mean(),data_slice.max())) data_slice = np.clip(data_slice, 0.0, 0.95) if options.loss_function == 'malis': components_slice, ccSizes = malis.connected_components_affgraph( label_slice.astype(int32), data_arrays[dataset]['nhood']) # Also recomputing the corresponding labels (connected components) net_io.setInputs([ data_slice, label_slice, components_slice, data_arrays[0]['nhood'] ]) if options.loss_function == 'euclid': ###net_io.setInputs([data_slice, label_slice, error_scale(label_slice,w_neg,w_pos)]) if (options.scale_error == 3): frac_pos = np.clip(label_slice.mean(), 0.01, 0.99) #for binary labels w_pos = 1.0 / (2.0 * frac_pos) w_neg = 1.0 / (2.0 * (1.0 - frac_pos)) net_io.setInputs([ data_slice, label_slice, error_scale(label_slice, w_neg, w_pos) ]) elif (options.scale_error == 1): frac_pos = weight_vec[0] w_pos = 1. / frac_pos label_weights = error_scale_overall(label_slice, weight_vec) net_io.setInputs([data_slice, label_slice, label_weights]) elif options.scale_error == 2: net_io.setInputs([data_slice, label_slice, weight_slice]) elif options.scale_error == 0: net_io.setInputs([data_slice, label_slice]) if options.loss_function == 'softmax': net_io.setInputs([data_slice, label_slice]) #pdb.set_trace() #print('training slice#: ', i_slice) # Single step n_slices = output_dims[-1] #loss = solver.stepForward(1) #loss = solver.stepParallel( n_slices ) loss = solver.stepForward(n_slices) solver.stepBackward(n_slices) #solver.stepBackward(1) i_slice = n_slices # do backward when all slices have been processed. #if i_slice == n_slices: # solver.stepBackward(1) #loss = solver.step(1) #n_slices) #for i in range(n_slices): # loss = solver.stepForward(1) #solver.stepBackward() # sanity_check_net_blobs(net) if (options.loss_function == 'euclid' or options.loss_function == 'euclid_aniso') and options.scale_error == 1: print("[Iter %i] Loss: %f, frac_pos=%f, w_pos=%f" % (i, loss, frac_pos, w_pos)) else: print("[Iter %i] Loss: %f" % (i, loss)) sys.stdout.flush() # TODO: Store losses to file losses += [loss] if hasattr(options, 'loss_snapshot') and ((i % options.loss_snapshot) == 0): io.savemat('loss.mat', {'loss': losses}) #pdb.set_trace() while gc.collect(): pass
plt.figure(str_title), plt.title(str_title) plt.imshow(data, cmap='gray') plt.axis('off') plt.rcParams['figure.figsize'] = (8, 8) # plt.rcParams['image.interpolation'] = 'nearest' plt.rcParams['image.cmap'] = 'gray' set_gpu_mode = 1 if set_gpu_mode: caffe.set_mode_gpu() caffe.set_device(0) #caffe.set_device(1) caffe.select_device(0, True) print("GPU mode") else: caffe.set_mode_cpu() print("CPU mode") net_file = ".\\data\\caffenet_places.prototxt" #caffe_model=".\\models\\caffenet_train_quick_iter_5000.caffemodel" caffe_model = ".\\models\\caffenet_train_quick_iter_6000.caffemodel" mean_bin = ".\\data\\mean.binaryproto" mean_npy = ".\\data\\mean.npy" convert_mean(mean_bin, mean_npy) imagenet_labels_filename = ".\\data\\synset_places.txt"
def main(argv): pycaffe_dir = os.path.dirname(__file__) parser = argparse.ArgumentParser() # Required arguments: input and output. parser.add_argument( "input_file", help="Input txt/csv filename. If .txt, must be list of filenames.\ If .csv, must be comma-separated file with header\ 'filename, xmin, ymin, xmax, ymax'" ) parser.add_argument( "output_file", help="Output h5/csv filename. Format depends on extension." ) # Optional arguments. parser.add_argument( "--model_def", default=os.path.join(pycaffe_dir, "../models/bvlc_reference_caffenet/deploy.prototxt"), help="Model definition file." ) parser.add_argument( "--pretrained_model", default=os.path.join(pycaffe_dir, "../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"), help="Trained model weights file." ) parser.add_argument( "--crop_mode", default="selective_search", choices=CROP_MODES, help="How to generate windows for detection." ) parser.add_argument( "--gpu", action='store_true', help="Switch for gpu computation." ) parser.add_argument( "--mean_file", default=os.path.join(pycaffe_dir, 'caffe/imagenet/ilsvrc_2012_mean.npy'), help="Data set image mean of H x W x K dimensions (numpy array). " + "Set to '' for no mean subtraction." ) parser.add_argument( "--input_scale", type=float, help="Multiply input features by this scale to finish preprocessing." ) parser.add_argument( "--raw_scale", type=float, default=255.0, help="Multiply raw input by this scale before preprocessing." ) parser.add_argument( "--channel_swap", default='2,1,0', help="Order to permute input channels. The default converts " + "RGB -> BGR since BGR is the Caffe default by way of OpenCV." ) parser.add_argument( "--context_pad", type=int, default='16', help="Amount of surrounding context to collect in input window." ) args = parser.parse_args() mean, channel_swap = None, None if args.mean_file: mean = np.load(args.mean_file) if mean.shape[1:] != (1, 1): mean = mean.mean(1).mean(1) if args.channel_swap: channel_swap = [int(s) for s in args.channel_swap.split(',')] if args.gpu: caffe.set_mode_gpu() caffe.set_devices((0,)) caffe.select_device(0, True) print("GPU mode") else: caffe.set_mode_cpu() print("CPU mode") # Make detector. detector = caffe.Detector(args.model_def, args.pretrained_model, mean=mean, input_scale=args.input_scale, raw_scale=args.raw_scale, channel_swap=channel_swap, context_pad=args.context_pad) # Load input. t = time.time() print("Loading input...") if args.input_file.lower().endswith('txt'): with open(args.input_file) as f: inputs = [_.strip() for _ in f.readlines()] elif args.input_file.lower().endswith('csv'): inputs = pd.read_csv(args.input_file, sep=',', dtype={'filename': str}) inputs.set_index('filename', inplace=True) else: raise Exception("Unknown input file type: not in txt or csv.") # Detect. if args.crop_mode == 'list': # Unpack sequence of (image filename, windows). images_windows = [ (ix, inputs.iloc[np.where(inputs.index == ix)][COORD_COLS].values) for ix in inputs.index.unique() ] detections = detector.detect_windows(images_windows) else: detections = detector.detect_selective_search(inputs) print("Processed {} windows in {:.3f} s.".format(len(detections), time.time() - t)) # Collect into dataframe with labeled fields. df = pd.DataFrame(detections) df.set_index('filename', inplace=True) df[COORD_COLS] = pd.DataFrame( data=np.vstack(df['window']), index=df.index, columns=COORD_COLS) del(df['window']) # Save results. t = time.time() if args.output_file.lower().endswith('csv'): # csv # Enumerate the class probabilities. class_cols = ['class{}'.format(x) for x in range(NUM_OUTPUT)] df[class_cols] = pd.DataFrame( data=np.vstack(df['feat']), index=df.index, columns=class_cols) df.to_csv(args.output_file, cols=COORD_COLS + class_cols) else: # h5 df.to_hdf(args.output_file, 'df', mode='w') print("Saved to {} in {:.3f} s.".format(args.output_file, time.time() - t))