def run(self): (train_data, test_data) = mnist_io.load_mb_from_mat(self.data_file, self.mb_size) np.set_printoptions(linewidth=200) num_test_samples = test_data[0].shape[0] (test_samples, test_labels) = test_data count = 1 for epoch in range(self.num_epochs): print '---Start epoch #%d' % epoch # train for (mb_samples, mb_labels) in train_data: num_samples = mb_samples.shape[0] a1 = mb_samples.T target = mb_labels.T # ff a2 = relu(np.dot(self.w1, a1) + self.b1) a3 = np.dot(self.w2, a2) + self.b2 # softmax & error out = softmax(a3) s3 = out - target # bp s2 = np.dot(self.w2.T, s3) s2 = relu_back(s2, a2) # grad gw1 = np.dot(s2, a1.T) / num_samples gb1 = np.sum(s2, axis=1, keepdims=True) / num_samples gw2 = np.dot(s3, a2.T) / num_samples gb2 = np.sum(s3, axis=1, keepdims=True) / num_samples # update self.w1 -= self.eps_w * gw1 self.w2 -= self.eps_w * gw2 self.b1 -= self.eps_b * gb1 self.b2 -= self.eps_b * gb2 if (count % 40 == 0): correct = np.max_index(out, axis=0) - np.max_index(target, axis=0) print 'Training error:', float( np.count_nonzero(correct)) / num_samples count = count + 1 # test a1 = test_samples.T a2 = relu(np.dot(self.w1, a1) + self.b1) a3 = np.dot(self.w2, a2) + self.b2 correct = np.max_index(a3, axis=0) - np.max_index(test_labels.T, axis=0) #print correct print 'Testing error:', float( np.count_nonzero(correct)) / num_test_samples print '---Finish epoch #%d' % epoch
def run(self): (train_data, test_data) = mnist_io.load_mb_from_mat(self.data_file, self.mb_size) np.set_printoptions(linewidth=200) num_test_samples = test_data[0].shape[0] (test_samples, test_labels) = test_data count = 1 for epoch in range(self.num_epochs): print '---Start epoch #%d' % epoch # train for (mb_samples, mb_labels) in train_data: num_samples = mb_samples.shape[0] a1 = mb_samples.T target = mb_labels.T # ff a2 = relu(np.dot(self.w1, a1) + self.b1) a3 = np.dot(self.w2, a2) + self.b2 # softmax & error out = softmax(a3) s3 = out - target # bp s2 = np.dot(self.w2.T, s3) s2 = relu_back(s2, a2) # grad gw1 = np.dot(s2, a1.T) / num_samples gb1 = np.sum(s2, axis=1, keepdims=True) / num_samples gw2 = np.dot(s3, a2.T) / num_samples gb2 = np.sum(s3, axis=1, keepdims=True) / num_samples # update self.w1 -= self.eps_w * gw1 self.w2 -= self.eps_w * gw2 self.b1 -= self.eps_b * gb1 self.b2 -= self.eps_b * gb2 if (count % 40 == 0): correct = np.max_index(out, axis=0) - np.max_index(target, axis=0) print 'Training error:', float(np.count_nonzero(correct)) / num_samples count = count + 1 # test a1 = test_samples.T a2 = relu(np.dot(self.w1, a1) + self.b1) a3 = np.dot(self.w2, a2) + self.b2 correct = np.max_index(a3, axis=0) - np.max_index(test_labels.T, axis=0) #print correct print 'Testing error:', float(np.count_nonzero(correct)) / num_test_samples print '---Finish epoch #%d' % epoch
def run(s): #Need Attention, here we may have multiple data layer, just choose the TEST layer data_unit = None for data_idx in range(len(s.owl_net.data_layers)): for i in range(len(s.owl_net.name_to_uid[s.owl_net.data_layers[data_idx]])): if s.owl_net.units[s.owl_net.name_to_uid[s.owl_net.data_layers[data_idx]][i]].params.include[0].phase == 1: data_unit = s.owl_net.units[s.owl_net.name_to_uid[s.owl_net.data_layers[data_idx]][i]] assert(data_unit) bp = BlobProto() #get mean file if len(data_unit.params.transform_param.mean_file) == 0: mean_data = np.ones([3, 256, 256], dtype=np.float32) assert(len(data_unit.params.transform_param.mean_value) == 3) mean_data[0] = data_unit.params.transform_param.mean_value[0] mean_data[1] = data_unit.params.transform_param.mean_value[1] mean_data[2] = data_unit.params.transform_param.mean_value[2] h_w = 256 else: with open(data_unit.params.transform_param.mean_file, 'rb') as f: bp.ParseFromString(f.read()) mean_narray = np.array(bp.data, dtype=np.float32) h_w = np.sqrt(np.shape(mean_narray)[0] / 3) mean_data = np.array(bp.data, dtype=np.float32).reshape([3, h_w, h_w]) #get the cropped img crop_size = data_unit.params.transform_param.crop_size crop_h_w = (h_w - crop_size) / 2 mean_data = mean_data[:, crop_h_w:crop_h_w + crop_size, crop_h_w:crop_h_w + crop_size] feature_unit = s.owl_net.units[s.owl_net.name_to_uid[s.layer_name][0]] batch_dir = 0 #we use 10000 images to conduct visualization all_data = np.zeros([10000, 3, crop_size, crop_size], dtype=np.float32) feature_shape = feature_unit.out_shape all_feature = np.zeros([10000, feature_shape[2], feature_shape[1], feature_shape[0]], dtype=np.float32) print 'Begin Generating Activations from Testing Set' curimg = 0 for testiteridx in range(s.owl_net.solver.test_iter[0]): s.owl_net.forward('TEST') feature = feature_unit.out.to_numpy() batch_size = np.shape(feature)[0] all_feature[curimg:curimg+batch_size,:] = feature data = data_unit.out.to_numpy() all_data[curimg:curimg+batch_size,:] = data curimg += batch_size #HACK TODO: only take 10000 images if curimg >= 10000: break info = 'Now Processed %d images' % (curimg) print info print 'Begin Selecting Patches' #get the result patch_shape = feature_unit.rec_on_ori min_val = -float('inf') #add back the mean file for i in range(np.shape(all_data)[0]): all_data[i,:,:,:] += mean_data if len(feature_shape) == 4: #iter for each filter, for each filter, we choose nine patch from different image for i in range(feature_shape[2]): #create the result image for nine patches res_img = np.zeros([feature_unit.rec_on_ori * 3, feature_unit.rec_on_ori * 3, 3]) filter_feature = np.copy(all_feature[:,i,:,:]) for patchidx in range(9): maxidx = np.argmax(filter_feature) colidx = maxidx % feature_shape[0] maxidx = (maxidx - colidx) / feature_shape[0] rowidx = maxidx % feature_shape[1] maxidx = (maxidx - rowidx) / feature_shape[1] imgidx = maxidx info = '%d %d %d' % (imgidx, rowidx, colidx) filter_feature[imgidx,:,:] = min_val #get the patch place patch_start_row = max(0,feature_unit.start_on_ori + rowidx * feature_unit.stride_on_ori) patch_end_row = min(feature_unit.start_on_ori + rowidx * feature_unit.stride_on_ori + feature_unit.rec_on_ori, data_unit.crop_size) if patch_start_row == 0: patch_end_row = feature_unit.rec_on_ori if patch_end_row == data_unit.crop_size: patch_start_row = data_unit.crop_size - feature_unit.rec_on_ori patch_start_col = max(0,feature_unit.start_on_ori + colidx * feature_unit.stride_on_ori) patch_end_col = min(feature_unit.start_on_ori + colidx * feature_unit.stride_on_ori + feature_unit.rec_on_ori, data_unit.crop_size) if patch_start_col == 0: patch_end_col = feature_unit.rec_on_ori if patch_end_col == data_unit.crop_size: patch_start_col = data_unit.crop_size - feature_unit.rec_on_ori patch = all_data[imgidx, :, patch_start_row:patch_end_row, patch_start_col:patch_end_col] #save img to image row_in_res = patchidx / 3 col_in_res = patchidx % 3 st_row = row_in_res * patch_shape st_col = col_in_res * patch_shape #turn gbr into rgb res_img[st_row:st_row+patch_end_row - patch_start_row, st_col:st_col + patch_end_col - patch_start_col, 2] = patch[0,:,:] res_img[st_row:st_row+patch_end_row - patch_start_row, st_col:st_col + patch_end_col - patch_start_col, 1] = patch[1,:,:] res_img[st_row:st_row+patch_end_row - patch_start_row, st_col:st_col + patch_end_col - patch_start_col, 0] = patch[2,:,:] #save img res_img = Image.fromarray(res_img.astype(np.uint8)) res_path = '%s/%d.jpg' % (s.result_path, i) print res_path res_img.save(res_path, format = 'JPEG') else: #Fully Layers #iter for each filter, for each filter, we choose nine patch from different image print feature_shape for i in range(feature_shape[0]): #create the result image for nine patches res_img = np.zeros([data_unit.crop_size * 3, data_unit.crop_size * 3, 3]) filter_feature = np.copy(all_feature[:,i]) for patchidx in range(9): maxidx = np.max_index(filter_feature) imgidx = maxidx filter_feature[imgidx] = min_val #save img to image row_in_res = patchidx / 3 col_in_res = patchidx % 3 st_row = row_in_res * data_unit.crop_size st_col = col_in_res * data_unit.crop_size #turn gbr into rgb patch = all_data[imgidx,:,:,:] res_img[st_row:st_row+data_unit.crop_size,st_col:st_col+data_unit.crop_size, 2] = patch[0,:,:] res_img[st_row:st_row+data_unit.crop_size,st_col:st_col+data_unit.crop_size, 1] = patch[1,:,:] res_img[st_row:st_row+data_unit.crop_size,st_col:st_col+data_unit.crop_size, 0] = patch[2,:,:] #save img res_img = Image.fromarray(res_img.astype(np.uint8)) res_path = '%s/%d.jpg' % (s.result_path, i) print res_path res_img.save(res_path, format = 'JPEG')