def main(): # import dataset dataset = pd.read_csv('Churn_Modelling.csv') # prepareDate X, Y = utils.prepareData(dataset) # create model model = utils.getModel() # compile model model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) #model.fit(X, Y, validation_split = 0.33, batch_size = 10, epochs = 100) # Split the dataset into the Training set(80%) and Test set(20%) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2) # fit the model model.fit(X_train, Y_train, validation_data = (X_test, Y_test), batch_size = 10, epochs = 100) # evaluate the model scores = model.evaluate(X_test, X_test) print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
def __init__(self, model, path, split="", category_name="Car", regress="GAUSSIAN", sigma_Gaussian=1, offset_BB=0, scale_BB=1.0): super().__init__( model=model, path=path, split=split, category_name=category_name, regress=regress, offset_BB=offset_BB, scale_BB=scale_BB) self.sigma_Gaussian = sigma_Gaussian self.offset_BB = offset_BB self.scale_BB = scale_BB self.num_candidates_perframe = 147 logging.info("preloading PC...") self.list_of_PCs = [None] * len(self.list_of_anno) self.list_of_BBs = [None] * len(self.list_of_anno) for index in tqdm(range(len(self.list_of_anno))): anno = self.list_of_anno[index] PC, box = self.getBBandPC(anno) new_PC = utils.cropPC(PC, box, offset=10) self.list_of_PCs[index] = new_PC self.list_of_BBs[index] = box logging.info("PC preloaded!") logging.info("preloading Model..") self.model_PC = [None] * len(self.list_of_tracklet_anno) for i in tqdm(range(len(self.list_of_tracklet_anno))): list_of_anno = self.list_of_tracklet_anno[i] PCs = [] BBs = [] cnt = 0 for anno in list_of_anno: this_PC, this_BB = self.getBBandPC(anno) PCs.append(this_PC) BBs.append(this_BB) anno["model_idx"] = i anno["relative_idx"] = cnt cnt += 1 self.model_PC[i] = getModel( PCs, BBs, offset=self.offset_BB, scale=self.scale_BB) logging.info("Model preloaded!")
def test000_testDenseNet121Output_withLoss_noException(self): test_batch = 1 test_loader, _, _ = get_zipped_dataloaders(self.TEST_DATASET_PATH, test_batch) test_criterion = nn.CrossEntropyLoss() model = getModel('densenet121') for i, (img, target) in enumerate(test_loader): output = model(img) test_criterion(output, target) if i == 0: break
def main(job_id, params): from utils import getModel from utils import getDataset logger = get_module_logger(__name__) logger.info('Starting job with id: %d' % job_id) logger.info('Fetching dataset %s' % params['dataset']) dataset = getDataset(params['dataset'])() logger.info('Using model %s' % params['experiment_name']) simulation = getModel(params['experiment_name'])(**params) logger.info('Starting training ...') score = simulation.run(dataset) return score
def runSpeedBench(args, arch: str, max_classifications: int) ->float: model = getModel(arch) model.setMaxClassifiers(max_classifications) tensor = torch.rand(1, 3, 224, 224) # warmup temp_res = model(tensor) start = timer() temp_res = model(tensor) end = timer() return (end - start) * 1000
def runQualityBench(args, arch_name: str, max_classification: int, loader) -> (float, float, float, float): loader = getDataLoader(args) label_to_classes = getLabelToClassMapping(os.path.join(os.getcwd(), args.data_root)) model = getModel(arch_name) if torch.cuda.is_available(): model = torch.nn.DataParallel(model).cuda() model.setMaxClassifiers(max_classification) model, _ , _, _ = resumeFromPath(os.path.join(args.state_path, f'{arch_name}_model_best.pth.tar'), model) model.eval() predT, grndT = evaluateModel(args, model, loader, label_to_classes) return getClassificationValues(predT, grndT)
def judge(): time.sleep(0.1) raw_sentence = args.text # raw_sentence = "K tell me anything about you." inputvec = getInputvec(raw_sentence, config) clf = getModel('CNN', config) pred = clf.predict(inputvec) pred_label = TOy_pred_label(pred) pred_label = pred_label.tolist() result = "".join(pred_label) if result == 'ham': result = '正常邮件' else: result = '垃圾邮件' return result
def executeClassificationReportBench(args, model_max: Tuple[str, int]): loader = getDataLoader(args) arch_name = f'{model_max[0]}{model_max[1]}' label_to_classes = getLabelToClassMapping(os.path.join(os.getcwd(), args.data_root)) model = getModel(arch_name) if torch.cuda.is_available(): model = torch.nn.DataParallel(model).cuda() model, _, _, _ = resumeFromPath(os.path.join(args.state_path, f'{arch_name}_model_best.pth.tar'), model) stats = { 'classifier': [], 'arch': [], 'acc': [], 'prec': [], 'rec': [], 'f1': [], 'ground_truth': [], 'prediction': [] } for max_classifications in range(1, model_max[1] + 1): logging.info(f'Running with Classification on Layer {max_classifications}') model.setMaxClassifiers(max_classifications) model.eval() pred, grndT = evaluateModel(args, model, loader, label_to_classes) acc, prec, rec, f1 = getClassificationValues(pred, grndT) stats['classifier'].append(max_classifications) stats['arch'].append(arch_name) stats['acc'].append(acc) stats['prec'].append(prec) stats['rec'].append(rec) stats['f1'].append(f1) stats['ground_truth'].append(grndT) stats['prediction'].append(pred) storeReportToCSV(args.report_path, f'report-{arch_name}-run.csv', stats)
def test(model_name): model = getModel(model_name=model_name) test_loader = dt.loadData(train=False) test_loss = 0 correct = 0 print("\nLoad saved model [", model_name, "]...") if not cfg.pretrained: model.load_state_dict( torch.load(cfg.log_path + model_name + "_cifar10.pt")['model_state_dict']) model.eval() print("Done..!") criterion = nn.CrossEntropyLoss() print("\nStart to test ", model_name, " ...") with torch.no_grad(): for data, target in tqdm(test_loader): data, target = data.to(cfg.device), target.to(cfg.device) if cfg.convert_to_RGB: batch_size, channel, width, height = data.size() data = data.view(batch_size, channel, width, height).expand(batch_size, cfg.converted_channel, width, height) output = model(data) test_loss += criterion(output, target).item() # sum up batch loss pred = output.argmax( dim=1, keepdim=True) # get the index of the max log-probability correct += pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset) print('\tTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n\n'. format(test_loss, correct, len(test_loader.dataset), 100. * correct / len(test_loader.dataset)))
def main(): # import dataset dataset = pd.read_csv('Churn_Modelling.csv') # prepare data set X, Y = utils.prepareData(dataset) print(X.shape) # create model model = utils.getModel() # compile model model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) # fit the model model.fit(X, Y, batch_size = 10, epochs = 100) model.summary() # evaluate the model scores = model.evaluate(X, Y) # print accuracy print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) Y_predict = model.predict(X) #Y_predict = (Y_predict > 0.5) print(type(Y_predict[:10])) # print head first 10 rows print(Y_predict[:100]) # print head first 10 rows print(Y[:100]) from sklearn.metrics import confusion_matrix cm = confusion_matrix(Y, Y_predict) print(cm)
from models import VGG16, I2V from utils import read_image, save_image, parseArgs, getModel, add_mean import argparse import time content_image_path, style_image_path, params_path, modeltype, width, alpha, beta, num_iters, device, optimize_simply, args = parseArgs() # The actual calculation print "Read images..." content_image = read_image(content_image_path, width) style_image = read_image(style_image_path, width) g = tf.Graph() with g.device(device), g.as_default(), tf.Session(graph=g, config=tf.ConfigProto(allow_soft_placement=True)) as sess: print "Load content values..." image = tf.constant(content_image) model = getModel(image, params_path, modeltype) content_image_y_val = [sess.run(y_l) for y_l in model.y()] # sess.run(y_l) is a constant numpy array print "Load style values..." image = tf.constant(style_image) model = getModel(image, params_path, modeltype) y = model.y() style_image_st_val = [] for l in range(len(y)): num_filters = content_image_y_val[l].shape[3] st_shape = [-1, num_filters] st_ = tf.reshape(y[l], st_shape) st = tf.matmul(tf.transpose(st_), st_) style_image_st_val.append(sess.run(st)) # sess.run(st) is a constant numpy array print "Construct graph..."
def main(args): # Plotting parameters params = { 'legend.fontsize': 'x-large', 'figure.figsize': (15, 5), 'axes.labelsize': 'x-large', 'axes.titlesize': 'x-large', 'xtick.labelsize': 'x-large', 'ytick.labelsize': 'x-large', 'figure.max_open_warning': 1000 } pylab.rcParams.update(params) # Create Model if "Random" in args.model_name: AE_chkpt_file = "" chkpt_file = None if "PreTrained" in args.model_name: AE_chkpt_file = os.path.join("..", "models", "Pretrain_ShapeNet", "model.pth.tar") chkpt_file = None if "Ours" in args.model_name: AE_chkpt_file = os.path.join("..", "models", "Pretrain_ShapeNet", "model.pth.tar") chkpt_file = os.path.join("..", "models", "Ours", "model.pth.tar") model = Model(128, chkpt_file=chkpt_file, AE_chkpt_file=AE_chkpt_file).cuda() model.eval() # Create Dataset dataset = SiameseTest(model=model, path=args.path_KITTI, split="Test", scale_BB=1.25) # Create search space x_space = np.linspace(-4, 4, 9) # min, max, number of sample y_space = np.linspace(-4, 4, 9) # min, max, number of sample a_space = np.linspace(-10, 10, 3) # min, max, number of sample X, Y, A = np.meshgrid(x_space, y_space, a_space) # create mesh grid dataset.search_grid = np.array([X.flatten(), Y.flatten(), A.flatten()]).T dataset.num_candidates_perframe = len(dataset.search_grid) # Get List of PCs and BBs from dataset PCs, BBs, list_of_anno = dataset[args.track] print("Length of candidates = ", len(dataset.search_grid), " length of pointclouds in the tracklet", len(PCs)) # Loop over Point Cloud and Bounding Boxes for i in tqdm(range(1, len(PCs))): this_PC = PCs[i] this_BB = BBs[i] ref_BB = BBs[i] # create candidate PCs and BBs search_space = dataset.search_grid candidate_BBs = generate_boxes(ref_BB, search_space=search_space) candidate_PCs = [ cropAndCenterPC(this_PC, box, offset=dataset.offset_BB, scale=dataset.scale_BB, normalize="PreTrained" in args.model_name) for box in candidate_BBs ] candidate_PCs_reg = [regularizePC(PC, model) for PC in candidate_PCs] candidate_PCs_torch = torch.cat(candidate_PCs_reg, dim=0).cuda() # create model PC model_PC = getModel(PCs[:i], BBs[:i], offset=dataset.offset_BB, scale=dataset.scale_BB, normalize="PreTrained" in args.model_name) # ax = fig.add_subplot(111, projection='3d') # ax.scatter(model_pc[] ys, zs, marker=m) repeat_shape = np.ones(len(candidate_PCs_torch.shape), dtype=np.int32) repeat_shape[0] = len(candidate_PCs) model_PC_encoded = regularizePC(model_PC, model).repeat( tuple(repeat_shape)).cuda() # infer model output, model_PC_decoded = model(candidate_PCs_torch, model_PC_encoded) model_PC_decoded = PointCloud( model_PC_decoded.detach().cpu().numpy()[0]) if "PreTrained" in args.model_name: normalizer = [ref_BB.wlh[1], ref_BB.wlh[0], ref_BB.wlh[2]] model_PC_decoded.points = model_PC_decoded.points * np.atleast_2d( normalizer).T # store scores for all candidates scores = output.detach().cpu().numpy() idx = np.argmax(scores) # select index of higest score box = candidate_BBs[idx] # select box with highest score # keep highest score for angle space scores0 = scores[1::3] # angle = 0 scoresp10 = scores[2::3] # angle = 10 scoresm10 = scores[0::3] # angle = -10 scores = np.max(np.stack([scores0, scoresp10, scoresm10]), axis=0) X, Y = np.meshgrid(x_space, y_space) Z = scores.reshape(len(x_space), len(y_space)) view_PC = cropAndCenterPC(this_PC, this_BB, offset=5) view_BB = Box([0, 0, 0], this_BB.wlh, Quaternion()) # Crop point clouds around GT x_filt_max = view_PC.points[0, :] < 5 x_filt_min = view_PC.points[0, :] > -5 y_filt_max = view_PC.points[1, :] < 5 y_filt_min = view_PC.points[1, :] > -5 z_filt_max = view_PC.points[2, :] < 2 z_filt_min = view_PC.points[2, :] > -1 close = np.logical_and(x_filt_min, x_filt_max) close = np.logical_and(close, y_filt_min) close = np.logical_and(close, y_filt_max) close = np.logical_and(close, z_filt_min) close = np.logical_and(close, z_filt_max) view_PC = PointCloud(view_PC.points[:, close]) # Create figure for TRACKING fig = plt.figure(figsize=(15, 10), facecolor="white") # Create axis in 3D ax = fig.gca(projection='3d') # Scatter plot the cropped point cloud ratio = 1 ax.scatter(view_PC.points[0, ::ratio], view_PC.points[1, ::ratio], view_PC.points[2, ::ratio] / 2 - 1, s=3, c=view_PC.points[2, ::ratio]) # point order to draw a full Box order = [0, 4, 0, 1, 5, 1, 2, 6, 2, 3, 7, 3, 0, 4, 5, 6, 7, 4] # Plot best results ax.plot(view_BB.corners()[0, order], view_BB.corners()[1, order], view_BB.corners()[2, order] / 2 - 1, color="red", alpha=0.5, linewidth=5, linestyle=":") # center the box to plot the GT box.translate(-this_BB.center) box.rotate(this_BB.orientation.inverse) # Plot GT box ax.plot(box.corners()[0, order], box.corners()[1, order], box.corners()[2, order] / 2 - 1, color="blue", alpha=0.5, linewidth=2, linestyle=":") # point order to draw the visible part of the box # order = [3, 0, 1, 2, 3, 7, 4, 0, 4, 5, 1] order = [6, 7, 4, 5, 6, 2, 1, 5, 1, 0, 4] # Plot best results ax.plot(view_BB.corners()[0, order], view_BB.corners()[1, order], view_BB.corners()[2, order] / 2 - 1, color="red", linewidth=5) # Plot GT box ax.plot(box.corners()[0, order], box.corners()[1, order], box.corners()[2, order] / 2 - 1, color="blue", linewidth=2) # Plot results for all cadidate as a surface surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, rstride=1, cstride=1, linewidth=0.3, edgecolors=[0, 0, 0, 0.8], antialiased=True, vmin=0, vmax=1) ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # Add a color bar which maps values to colors. fig.colorbar(surf, shrink=0.5, aspect=5) ax.w_xaxis.set_pane_color((0.9, 0.9, 0.9, 0.2)) ax.w_yaxis.set_pane_color((0.9, 0.9, 0.9, 0.2)) ax.w_zaxis.set_pane_color((0.9, 0.9, 0.9, 0.2)) ax.w_xaxis.line.set_color((1.0, 1.0, 1.0, 0.0)) ax.w_yaxis.line.set_color((1.0, 1.0, 1.0, 0.0)) ax.w_zaxis.line.set_color((1.0, 1.0, 1.0, 0.0)) ax.set_xticks(x_space[::2]) ax.set_yticks(y_space[::2]) ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_zticks([0, 0.2, 0.4, 0.6, 0.8, 1]) ax.view_init(20, -140) plt.tight_layout() ax.set_xlim(-5, 5) ax.set_ylim(-5, 5) ax.set_zlim(-1.5, 1) # Save figure as Tracking results os.makedirs(os.path.join(args.path_results, f"{args.track:04.0f}", "Tracking"), exist_ok=True) plt.savefig(os.path.join(args.path_results, f"{args.track:04.0f}", "Tracking", f"{i}_{args.model_name}.png"), format='png', dpi=100) # Create figure for RECONSTRUCTION fig = plt.figure(figsize=(9, 6), facecolor="white") # Create axis in 3D ax = fig.gca(projection='3d') # select 2K point to visualize sample = np.random.randint(low=0, high=model_PC_decoded.points.shape[1], size=2048, dtype=np.int64) print("Reconstructed size", model_PC_decoded.points.shape[1]) # Scatter plot the point cloud # ax.scatter( # model_PC_decoded.points[0, sample], # model_PC_decoded.points[1, sample], # model_PC_decoded.points[2, sample], # s=3, # c=model_PC_decoded.points[2, sample]) ax.scatter(model_PC_decoded.points[0, :], model_PC_decoded.points[1, :], model_PC_decoded.points[2, :], s=3, c=model_PC_decoded.points[2, sample]) # Plot the car BB order = [0, 4, 0, 1, 5, 1, 2, 6, 2, 3, 7, 3, 0, 4, 5, 6, 7, 4] ax.plot(view_BB.corners()[0, order], view_BB.corners()[1, order], view_BB.corners()[2, order], color="red", alpha=0.5, linewidth=3, linestyle=":") order = [6, 7, 4, 5, 6, 2, 1, 5, 1, 0, 4] ax.plot(view_BB.corners()[0, order], view_BB.corners()[1, order], view_BB.corners()[2, order], color="red", linewidth=3) # setup axis ax.set_xticks([-2, -1, 0, 1, 2]) ax.set_xticklabels([], fontsize=10) ax.set_yticks([-1, 0, 1]) ax.set_yticklabels([], fontsize=10) ax.set_zticks([-1, 0, 1]) ax.set_zticklabels([], fontsize=10) ax.view_init(20, -140) plt.tight_layout() ax.set_xlim(-2, 2) ax.set_ylim(-2, 2) ax.set_zlim(-1.5, 1.5) # Save figure as Decoded Model results os.makedirs(os.path.join(args.path_results, f"{args.track:04.0f}", "Reconstruction"), exist_ok=True) plt.savefig(os.path.join(args.path_results, f"{args.track:04.0f}", "Reconstruction", f"{i}_{args.model_name}.png"), format='png', dpi=100) # Create figure for MODEL PC fig = plt.figure(figsize=(9, 6), facecolor="white") ax = fig.gca(projection='3d') if "PreTrained" in args.model_name: model_PC = getModel(PCs[:i], BBs[:i], offset=dataset.offset_BB, scale=dataset.scale_BB) # sample 2K Points sample = np.random.randint(low=0, high=model_PC.points.shape[1], size=2048, dtype=np.int64) # Scatter plot the Point cloud ax.scatter(model_PC.points[0, sample], model_PC.points[1, sample], model_PC.points[2, sample], s=3, c=model_PC.points[2, sample]) # Plot the Bounding Box order = [0, 4, 0, 1, 5, 1, 2, 6, 2, 3, 7, 3, 0, 4, 5, 6, 7, 4] ax.plot(view_BB.corners()[0, order], view_BB.corners()[1, order], view_BB.corners()[2, order], color="red", alpha=0.5, linewidth=3, linestyle=":") order = [6, 7, 4, 5, 6, 2, 1, 5, 1, 0, 4] ax.plot(view_BB.corners()[0, order], view_BB.corners()[1, order], view_BB.corners()[2, order], color="red", linewidth=3) # setup axis ax.set_xticks([-2, -1, 0, 1, 2]) ax.set_xticklabels([], fontsize=10) ax.set_yticks([-1, 0, 1]) ax.set_yticklabels([], fontsize=10) ax.set_zticks([-1, 0, 1]) ax.set_zticklabels([], fontsize=10) ax.view_init(20, -140) plt.tight_layout() ax.set_xlim(-2, 2) ax.set_ylim(-2, 2) ax.set_zlim(-1.5, 1.5) ax.set_zlabel('Model') # Save figure as Model results os.makedirs(os.path.join(args.path_results, f"{args.track:04.0f}", "Model"), exist_ok=True) plt.savefig(os.path.join(args.path_results, f"{args.track:04.0f}", "Model", f"{i}_{args.model_name}.png"), format='png', dpi=100) # create GIF Tracking images = [] for i in tqdm(range(1, len(PCs))): image_path = os.path.join(args.path_results, f'{args.track:04.0f}', "Tracking", f'{i}_{args.model_name}.png') images.append(imageio.imread(image_path)) image_path = os.path.join( args.path_results, f'{args.track:04.0f}_{args.model_name}_Tracking.gif') imageio.mimsave(image_path, images) # create GIF GT Car images = [] for i in tqdm(range(1, len(PCs))): image_path = os.path.join(args.path_results, f'{args.track:04.0f}', "Model", f'{i}_{args.model_name}.png') images.append(imageio.imread(image_path)) image_path = os.path.join( args.path_results, f'{args.track:04.0f}_{args.model_name}_Model.gif') imageio.mimsave(image_path, images) # create GIF Reconstructed Car images = [] for i in tqdm(range(1, len(PCs))): image_path = os.path.join(args.path_results, f'{args.track:04.0f}', "Reconstruction", f'{i}_{args.model_name}.png') images.append(imageio.imread(image_path)) image_path = os.path.join( args.path_results, f'{args.track:04.0f}_{args.model_name}_Reconstruction.gif') imageio.mimsave(image_path, images)
def produce_art(content_image_path, style_image_path, model_path, model_type, width, alpha, beta, num_iters): # The actual calculation print "Read images..." content_image = read_image(content_image_path, width) style_image = read_image(style_image_path, width) g = tf.Graph() with g.device(device), g.as_default(), tf.Session(graph=g, config=tf.ConfigProto(allow_soft_placement=True)) as sess: print "Load content values..." image = tf.constant(content_image) model = getModel(image, model_path, model_type) content_image_y_val = [sess.run(y_l) for y_l in model.y()] # sess.run(y_l) is a constant numpy array print "Load style values..." image = tf.constant(style_image) model = getModel(image, model_path, model_type) y = model.y() style_image_st_val = [] for l in range(len(y)): num_filters = content_image_y_val[l].shape[3] st_shape = [-1, num_filters] st_ = tf.reshape(y[l], st_shape) st = tf.matmul(tf.transpose(st_), st_) style_image_st_val.append(sess.run(st)) # sess.run(st) is a constant numpy array print "Construct graph..." # Start from white noise # gen_image = tf.Variable(tf.truncated_normal(content_image.shape, stddev=20), trainable=True, name='gen_image') # Start from the original image gen_image = tf.Variable(tf.constant(np.array(content_image, dtype=np.float32)), trainable=True, name='gen_image') model = getModel(gen_image, model_path, model_type) y = model.y() L_content = 0.0 L_style = 0.0 for l in range(len(y)): # Content loss L_content += model.alpha[l] * tf.nn.l2_loss(y[l] - content_image_y_val[l]) # Style loss num_filters = content_image_y_val[l].shape[3] st_shape = [-1, num_filters] st_ = tf.reshape(y[l], st_shape) st = tf.matmul(tf.transpose(st_), st_) N = np.prod(content_image_y_val[l].shape).astype(np.float32) L_style += model.beta[l] * tf.nn.l2_loss(st - style_image_st_val[l]) / N ** 2 / len(y) # The loss L = alpha * L_content + beta * L_style # The optimizer global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(learning_rate=2.0, global_step=global_step, decay_steps=100, decay_rate=0.94, staircase=True) train_step = tf.train.AdamOptimizer(learning_rate).minimize(L, global_step=global_step) # A more simple optimizer # train_step = tf.train.AdamOptimizer(learning_rate=2.0).minimize(L) # Set up the summary writer (saving summaries is optional) # (do `tensorboard --logdir=/tmp/na-logs` to view it) tf.scalar_summary("L_content", L_content) tf.scalar_summary("L_style", L_style) gen_image_addmean = tf.Variable(tf.constant(np.array(content_image, dtype=np.float32)), trainable=False) tf.image_summary("Generated image (TODO: add mean)", gen_image_addmean) summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter('/tmp/na-logs', graph_def=sess.graph_def) print "Start calculation..." # The optimizer has variables that require initialization as well sess.run(tf.initialize_all_variables()) for i in range(num_iters): if i % 10 == 0: gen_image_val = sess.run(gen_image) save_image(gen_image_val, i, out_dir) print "L_content, L_style:", sess.run(L_content), sess.run(L_style) # Increment summary sess.run(tf.assign(gen_image_addmean, add_mean(gen_image_val))) summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, i) print "Iter:", i sess.run(train_step)
test = not test return test from utils import getModel, predict # ning accuracy was 96.0%. # ing accuracy was 90.0% (N = 50). correct = 0.0 for ei in range(len(features)): # select all but the one at position `ei`: training = np.ones(len(features), bool) training[ei] = False testing = ~training model = getModel(features[training], is_virginica[training]) predict(model, features[testing]) predictions = predict(model, features[testing]) correct += np.sum(predictions == is_virginica[testing]) acc = correct / float(len(features)) print('Accuracy: {0:.1%}'.format(acc)) ########################################### ############## SEEDS DATASET ############## ########################################### from main.ch02.load import load_dataset feature_names = [ 'area',
from utils import getModel from option import getPredictParser import torch import time if __name__ == "__main__": parser = getPredictParser() args = parser.parse_args() net, _ = getModel(args, save_path=args.weight_path, mode="eval") input = torch.rand((1, 3, 320, 320)).cuda() start = time.time() for i in range(1000): output = net(input) end = time.time() print((end - start) / 1000)
print(f" Data directory: {ARGS.data_dir}") print(f" Checkpoint directory: {ARGS.save_dir}") print(f" Use GPU: {ARGS.gpu}") print() # Dont display warning for CUDA drivers warnings.filterwarnings("ignore", category=UserWarning) if ARGS.gpu: if torch.cuda.is_available(): utils.DEVICE = 'cuda' else: print("WARNING: cuda is not available, using cpu instead", file=sys.stderr) utils.DEVICE = 'cpu' loaders = getDataLoaders(ARGS.data_dir[0]) output_units = len(loaders['train'].dataset.class_to_idx) print(f"Training started") model = utils.getModel(ARGS.arch, ARGS.hidden_units, output_units) start = time.perf_counter() model.classifier.class_to_idx = loaders['train'].dataset.class_to_idx model = trainModel(model, loaders, ARGS.learning_rate, ARGS.epochs, ARGS.save_dir) stop = time.perf_counter() print(f"Training finished in {stop - start:0.4f} seconds") test_accuracy = accuracy_on_loader(model, loaders['test']) print(f"Model accuracy on testing data: {test_accuracy}")
help='features dir') parser.add_argument('-seq_length', type=int, required=True, help='sequences length') parser.add_argument('-cnn_type', type=str, required=True, help='features extractor cnn type') parser.add_argument('-gpu', action="store_true", help='use gpu or not') args = parser.parse_args() print(args.model) print(args.gpu) model = getModel(model_type=args.model, use_gpu=args.gpu) train_loader = getDataLoader(args.seq_dir, args.seq_dir + '/train_metadata.txt', args.seq_length, args.cnn_type) print('get train loader done') val_loader = getDataLoader(args.seq_dir, args.seq_dir + '/test_metadata.txt', args.seq_length, args.cnn_type) print('get val loader done') checkpoints_path = os.path.join(conf.CHECKPOINTS_PATH, args.model, datetime.now().isoformat()) if not os.path.exists(checkpoints_path): os.makedirs(checkpoints_path) checkpoints_path = os.path.join(checkpoints_path,
def test(loader, model, model_name="dummy_model", epoch=-1, shape_aggregation="", search_space="", number_candidate=125, reference_BB="", model_fusion="pointcloud", max_iter=-1, IoU_Space=3, DetailedMetrics=False): batch_time = AverageMeter() data_time = AverageMeter() Success_main = Success() Precision_main = Precision() Accuracy_Completeness_main = Accuracy_Completeness() Precision_occluded = [Precision(), Precision()] Success_occluded = [Success(), Success()] Precision_dynamic = [Precision(), Precision()] Success_dynamic = [Success(), Success()] # SEARCH SPACE INIT if ("Kalman".upper() in search_space.upper()): search_space_sampler = KalmanFiltering() elif ("Particle".upper() in search_space.upper()): search_space_sampler = ParticleFiltering() elif ("GMM".upper() in search_space.upper()): search_space_sampler = GaussianMixtureModel(n_comp=int(search_space[3:])) else: search_space_sampler = ExhaustiveSearch() # switch to evaluate mode model.eval() end = time.time() dataset = loader.dataset with tqdm(enumerate(loader), total=len(loader.dataset.list_of_anno), ncols=220) as t: for batch in loader: # measure data loading time data_time.update((time.time() - end)) for PCs, BBs, list_of_anno in batch: # tracklet search_space_sampler.reset() results_BBs = [] results_scores = [] results_latents = [] for i, _ in enumerate(PCs): this_anno = list_of_anno[i] this_BB = BBs[i] this_PC = PCs[i] # IS THE POINT CLOUD OCCLUDED? occluded = this_anno["occluded"] if occluded in [0]: # FULLY VISIBLE occluded = 0 elif occluded in [1, 2]: # PARTIALLY AND FULLY OCCLUDED occluded = 1 else: occluded = -1 # INITIAL FRAME if i == 0: box = BBs[i] model_PC = utils.getModel([this_PC], [this_BB], offset=dataset.offset_BB, scale=dataset.scale_BB) if "latent".upper() in model_fusion.upper(): this_latent = model.AE.encode( utils.regularizePC(model_PC, model).cuda())[0] score = 1.0 candidate_BBs = [] dynamic = -1 else: # previous_PC = PCs[i - 1] previous_BB = BBs[i - 1] # previous_anno = list_of_anno[i - 1] # IS THE SAMPLE dynamic? if (np.linalg.norm(this_BB.center - previous_BB.center) > 0.709): # for complete set dynamic = 1 else: dynamic = 0 # DEFINE REFERENCE BB if ("previous_result".upper() in reference_BB.upper()): ref_BB = results_BBs[-1] elif ("previous_gt".upper() in reference_BB.upper()): ref_BB = previous_BB elif ("current_gt".upper() in reference_BB.upper()): ref_BB = this_BB search_space = search_space_sampler.sample( number_candidate) candidate_BBs = utils.generate_boxes( ref_BB, search_space=search_space) candidate_PCs = [ utils.cropAndCenterPC( this_PC, box, offset=dataset.offset_BB, scale=dataset.scale_BB) for box in candidate_BBs ] candidate_PCs_reg = [ utils.regularizePC(PC, model) for PC in candidate_PCs ] candidate_PCs_torch = torch.cat( candidate_PCs_reg, dim=0).cuda() # DATA FUSION: PC vs LATENT if "latent".upper() in model_fusion.upper(): candidate_PCs_encoded = model.AE.encode(candidate_PCs_torch) model_PC_encoded = torch.stack(results_latents) # stack all latent vectors # AGGREGATION: IO vs ONLY0 vs ONLYI vs AVG vs MEDIAN vs MAX if ("firstandprevious".upper() in shape_aggregation.upper()): model_PC_encoded = (model_PC_encoded[0] + model_PC_encoded[i-1] )/ 2 elif "first".upper() in shape_aggregation.upper(): model_PC_encoded = model_PC_encoded[0] elif "previous".upper() in shape_aggregation.upper(): model_PC_encoded = model_PC_encoded[i-1] elif "MEDIAN".upper() in shape_aggregation.upper(): model_PC_encoded = torch.median(model_PC_encoded,0)[0] elif ("MAX".upper() in shape_aggregation.upper()): model_PC_encoded = torch.max(model_PC_encoded,0)[0] elif ("AVG".upper() in shape_aggregation.upper()): model_PC_encoded = torch.mean(model_PC_encoded,0) else: model_PC_encoded = torch.mean(model_PC_encoded,0) # repeat model_encoded for size similarity with candidate_PCs repeat_shape = np.ones(len(candidate_PCs_encoded.shape), dtype=np.int32) repeat_shape[0] = len(candidate_PCs_encoded) model_PC_encoded = model_PC_encoded.repeat(tuple(repeat_shape)).cuda() #TODO: remove torch dependency =-> Functional # Y_AE = model.AE.forward(prev_PC) output = F.cosine_similarity(candidate_PCs_encoded, model_PC_encoded, dim=1) scores = output.detach().cpu().numpy() elif "pointcloud".upper() in model_fusion.upper(): # AGGREGATION: IO vs ONLY0 vs ONLYI vs ALL if ("firstandprevious".upper() in shape_aggregation.upper()): model_PC = utils.getModel( [PCs[0], PCs[i - 1]], [results_BBs[0], results_BBs[i - 1]], offset=dataset.offset_BB, scale=dataset.scale_BB) elif ("first".upper() in shape_aggregation.upper()): model_PC = utils.getModel( [PCs[0]], [results_BBs[0]], offset=dataset.offset_BB, scale=dataset.scale_BB) elif ("previous".upper() in shape_aggregation. upper()): model_PC = utils.getModel( [PCs[i - 1]], [results_BBs[i - 1]], offset=dataset.offset_BB, scale=dataset.scale_BB) elif ("all".upper() in shape_aggregation.upper()): model_PC = utils.getModel( PCs[:i], results_BBs, offset=dataset.offset_BB, scale=dataset.scale_BB) else: model_PC = utils.getModel( PCs[:i], results_BBs, offset=dataset.offset_BB, scale=dataset.scale_BB) repeat_shape = np.ones( len(candidate_PCs_torch.shape), dtype=np.int32) repeat_shape[0] = len(candidate_PCs_torch) model_PC_encoded = utils.regularizePC( model_PC, model).repeat(tuple(repeat_shape)).cuda() output, decoded_PC = model(candidate_PCs_torch, model_PC_encoded) scores = output.detach().cpu().numpy() elif "space".upper() in model_fusion.upper(): scores = np.array([ utils.distanceBB_Gaussian(bb, this_BB) for bb in candidate_BBs ]) search_space_sampler.addData(data=search_space, score=scores.T) idx = np.argmax(scores) score = scores[idx] box = candidate_BBs[idx] if "latent".upper() in model_fusion.upper(): this_latent = candidate_PCs_encoded[idx] if(DetailedMetrics): # Construct GT model gt_model_PC_start_idx = max(0,i-10) gt_model_PC_end_idx = min(i+10,len(PCs)) gt_model_PC = utils.getModel( PCs[gt_model_PC_start_idx:gt_model_PC_end_idx], BBs[gt_model_PC_start_idx:gt_model_PC_end_idx], offset=dataset.offset_BB, scale=dataset.scale_BB) if(gt_model_PC.points.shape[1]>0): gt_model_PC = gt_model_PC.convertToPytorch().float().unsqueeze(2).permute(2,0,1) gt_candidate_PC = utils.regularizePC( utils.cropAndCenterPC( this_PC, this_BB, offset=dataset.offset_BB, scale=dataset.scale_BB), model).cuda() decoded_PC = model.AE.decode( model.AE.encode( gt_candidate_PC)).detach().cpu() Accuracy_Completeness_main.update( decoded_PC, gt_model_PC) results_BBs.append(box) results_scores.append(score) if "latent".upper() in model_fusion.upper(): results_latents.append(this_latent.detach().cpu()) # estimate overlap/accuracy fro current sample this_overlap = estimateOverlap(this_BB, box, dim=IoU_Space) this_accuracy = estimateAccuracy(this_BB, box, dim=IoU_Space) Success_main.add_overlap(this_overlap) Precision_main.add_accuracy(this_accuracy) if (dynamic >= 0): Success_dynamic[dynamic].add_overlap(this_overlap) Precision_dynamic[dynamic].add_accuracy(this_accuracy) if (occluded >= 0): Success_occluded[occluded].add_overlap(this_overlap) Precision_occluded[occluded].add_accuracy(this_accuracy) # measure elapsed time batch_time.update(time.time() - end) end = time.time() t.update(1) if Success_main.count >= max_iter and max_iter >= 0: return Success_main.average, Precision_main.average t.set_description(f'Test {epoch}: ' f'Time {batch_time.avg:.3f}s ' f'(it:{batch_time.val:.3f}s) ' f'Data:{data_time.avg:.3f}s ' f'(it:{data_time.val:.3f}s), ' f'Succ/Prec:' f'{Success_main.average:.1f}/' f'{Precision_main.average:.1f}') if DetailedMetrics: logging.info(f"Succ/Prec fully visible({Success_occluded[0].count}):") logging.info(f"{Success_occluded[0].average:.1f}/{Precision_occluded[0].average:.1f}") logging.info(f"Succ/Prec occluded({Success_occluded[1].count}):") logging.info(f"{Success_occluded[1].average:.1f}/{Precision_occluded[1].average:.1f}") logging.info(f"Succ/Prec dynamic({Success_dynamic[0].count}):") logging.info(f"{Success_dynamic[0].average:.1f}/{Precision_dynamic[0].average:.1f}") logging.info(f"Succ/Prec static({Success_dynamic[1].count}):") logging.info(f"{Success_dynamic[1].average:.1f}/{Precision_dynamic[1].average:.1f}") logging.info(f"Acc/Comp ({Accuracy_Completeness_main.count}):") logging.info(f"{Accuracy_Completeness_main.average[0]:.4f}/{Accuracy_Completeness_main.average[1]:.4f}") return Success_main.average, Precision_main.average
def train(model_name): # train model train_loader = dt.loadData(train=True) model = getModel(model_name = model_name) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=cfg.lr, momentum=cfg.momentum) scheduler = optim.lr_scheduler.StepLR(optimizer, cfg.lr_decaying_step, gamma = cfg.lr_decaying_value) saved_model_path = cfg.log_path + model_name + "_cifar10.pt" if os.path.isfile(saved_model_path): # load saved checkpoint checkpoint = torch.load(saved_model_path) model.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict']) start_epoch = checkpoint['epoch'] loss = checkpoint['loss'] else: start_epoch = 1 model.train() print("\nStart training ", model_name, "...") for epoch in range(start_epoch, cfg.epochs + 1): for batch_idx, (data, target) in enumerate(tqdm(train_loader)): data, target = data.to(cfg.device), target.to(cfg.device) if cfg.convert_to_RGB: batch_size, channel, width, height = data.size() data = data.view(batch_size, channel, width, height).expand(batch_size, cfg.converted_channel, width, height) optimizer.zero_grad() output=model(data) loss = criterion(output, target) loss.backward() optimizer.step() scheduler.step() if cfg.save_model and (epoch % cfg.save_epoch == 0): if not(os.path.isdir(cfg.log_path)): os.makedirs(os.path.join(cfg.log_path)) torch.save({ 'epoch':epoch + 1, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), 'loss': loss }, saved_model_path) print('\tTrain Epoch: {} / {} \t Loss: {:.6f}\n'.format(epoch, cfg.epochs, loss.item())) print("Done!\n\n")
parser.add_argument('-r', '--learning_rate', help='Learning rate', default=None) parser.add_argument('-i', '--test_incremental', help='If specified, will test on incremental dataset', action='store_true') args = parser.parse_args() computer = ut.getComputer(args.computer) environment = ut.getImEnv(args.environment) label_type = ut.getLabelType(args.label_type) gen_mode = ut.getGenMode(args.generation_mode) model_name = ut.getModelName(args.model_name) model = ut.getModel(args.model) learning_rate = args.learning_rate test_incremental = args.test_incremental config = ut.loadYAMLFromFile('config_' + environment + '.yaml') config['exp']['computer'] = computer config['exp']['label_type'] = label_type config['exp']['environment'] = environment config['exp']['test_incremental'] = test_incremental config['exp']['gen_mode'] = gen_mode config['exp']['model_name'] = model_name config['cnn']['model'] = model if learning_rate: config['cnn']['learning_rate'] = float(learning_rate)