def cls_im(self, im): im = im.astype(np.float32, copy=True) normalized_im = T.normalize(im, mean=self.mean, std=self.std) scale_im, scale_ratio = T.scale(normalized_im, short_size=self.base_size) crop_ims = [] if self.crop_type == 'center' or self.crop_type == 'single': # for single crop crop_ims.append(T.center_crop(scale_im, crop_size=self.crop_size)) elif self.crop_type == 'mirror' or self.crop_type == 'multi': # for 10 crops crop_ims.extend(T.mirror_crop(scale_im, crop_size=self.crop_size)) else: crop_ims.append(scale_im) scores = self.inference(np.asarray(crop_ims, dtype=np.float32), output_layer=self.prob_layer) return np.sum(scores, axis=0)
def eval_batch(): eval_len = len(SET_DICT) accuracy = np.zeros(len(args.top_k)) start_time = datetime.datetime.now() for i in xrange(eval_len - args.skip_num): im = cv2.imread(SET_DICT[i + args.skip_num]['path']) im = T.bgr2rgb(im) scale_im = T.pil_resize(Image.fromarray(im), args.base_size) normalized_im = T.normalize(np.asarray(scale_im) / 255.0, mean=PIXEL_MEANS, std=PIXEL_STDS) crop_ims = [] if args.crop_type == 'center': # for single crop crop_ims.append(T.center_crop(normalized_im, crop_size=args.crop_size)) elif args.crop_type == 'multi': # for 10 crops crop_ims.extend(T.mirror_crop(normalized_im, crop_size=args.crop_size)) else: crop_ims.append(normalized_im) score_vec = np.zeros(args.class_num, dtype=np.float32) iter_num = int(len(crop_ims) / args.batch_size) timer_pt1 = datetime.datetime.now() for j in xrange(iter_num): input_data = np.asarray(crop_ims, dtype=np.float32)[j * args.batch_size:(j + 1) * args.batch_size] input_data = input_data.transpose(0, 3, 1, 2) input_data = torch.autograd.Variable(torch.from_numpy(input_data).cuda(), volatile=True) outputs = MODEL(input_data) scores = outputs.data.cpu().numpy() score_vec += np.sum(scores, axis=0) score_index = (-score_vec / len(crop_ims)).argsort() - 1 timer_pt2 = datetime.datetime.now() SET_DICT[i + args.skip_num]['evaluated'] = True SET_DICT[i + args.skip_num]['score_vec'] = score_vec / len(crop_ims) print 'Testing image: {}/{} {} {}/{} {}s' \ .format(str(i + 1), str(eval_len - args.skip_num), str(SET_DICT[i + args.skip_num]['path'].split('/')[-1]), str(score_index[0]), str(SET_DICT[i + args.skip_num]['gt']), str((timer_pt2 - timer_pt1).microseconds / 1e6 + (timer_pt2 - timer_pt1).seconds)), for j in xrange(len(args.top_k)): if SET_DICT[i + args.skip_num]['gt'] in score_index[:args.top_k[j]]: accuracy[j] += 1 tmp_acc = float(accuracy[j]) / float(i + 1) if args.top_k[j] == 1: print '\ttop_' + str(args.top_k[j]) + ':' + str(tmp_acc), else: print 'top_' + str(args.top_k[j]) + ':' + str(tmp_acc) end_time = datetime.datetime.now() w = open(LOG_PTH, 'w') s1 = 'Evaluation process ends at: {}. \nTime cost is: {}. '.format(str(end_time), str(end_time - start_time)) s2 = '\nThe model is: {}. \nThe val file is: {}. \n{} images has been tested, crop_type is: {}, base_size is: {}, ' \ 'crop_size is: {}.'.format(args.model_weights, args.val_file, str(eval_len - args.skip_num), args.crop_type, str(args.base_size), str(args.crop_size)) s3 = '\nThe PIXEL_MEANS is: ({}, {}, {}), PIXEL_STDS is : ({}, {}, {}).' \ .format(str(PIXEL_MEANS[0]), str(PIXEL_MEANS[1]), str(PIXEL_MEANS[2]), str(PIXEL_STDS[0]), str(PIXEL_STDS[1]), str(PIXEL_STDS[2])) s4 = '' for i in xrange(len(args.top_k)): _acc = float(accuracy[i]) / float(eval_len - args.skip_num) s4 += '\nAccuracy of top_{} is: {}; correct num is {}.'.format(str(args.top_k[i]), str(_acc), str(int(accuracy[i]))) print s1, s2, s3, s4 w.write(s1 + s2 + s3 + s4) w.close() if args.save_score_vec: w = open(LOG_PTH.replace('.txt', 'scorevec.txt'), 'w') for i in xrange(eval_len - args.skip_num): w.write(SET_DICT[i + args.skip_num]['score_vec']) w.close() print('DONE!')
def eval_batch(): # shuffle_conv1_channel() eval_len = len(SET_DICT) # eval_len = 1000 accuracy = np.zeros(len(args.top_k)) start_time = datetime.datetime.now() for i in xrange(eval_len - args.skip_num): im = cv2.imread(SET_DICT[i + args.skip_num]['path']) if (PIXEL_MEANS == np.array([103.52, 116.28, 123.675])).all() and \ (PIXEL_STDS == np.array([57.375, 57.12, 58.395])).all(): scale_im = T.pil_scale(Image.fromarray(im), args.base_size) scale_im = np.asarray(scale_im) else: scale_im, _ = T.scale(im, short_size=args.base_size) input_im = T.normalize(scale_im, mean=PIXEL_MEANS, std=PIXEL_STDS) crop_ims = [] if args.crop_type == 'center': # for single crop crop_ims.append(T.center_crop(input_im, crop_size=args.crop_size)) elif args.crop_type == 'multi': # for 10 crops crop_ims.extend(T.mirror_crop(input_im, crop_size=args.crop_size)) else: crop_ims.append(input_im) score_vec = np.zeros(args.class_num, dtype=np.float32) iter_num = int(len(crop_ims) / args.batch_size) timer_pt1 = datetime.datetime.now() for j in xrange(iter_num): scores = CLS.inference( np.asarray(crop_ims, dtype=np.float32)[j * args.batch_size:(j + 1) * args.batch_size], output_layer=args.prob_layer ) score_vec += np.sum(scores, axis=0) score_index = (-score_vec / len(crop_ims)).argsort() timer_pt2 = datetime.datetime.now() SET_DICT[i + args.skip_num]['evaluated'] = True SET_DICT[i + args.skip_num]['score_vec'] = score_vec / len(crop_ims) print 'Testing image: {}/{} {} {}/{} {}s' \ .format(str(i + 1), str(eval_len - args.skip_num), str(SET_DICT[i + args.skip_num]['path'].split('/')[-1]), str(score_index[0]), str(SET_DICT[i + args.skip_num]['gt']), str((timer_pt2 - timer_pt1).microseconds / 1e6 + (timer_pt2 - timer_pt1).seconds)), for j in xrange(len(args.top_k)): if SET_DICT[i + args.skip_num]['gt'] in score_index[:args.top_k[j]]: accuracy[j] += 1 tmp_acc = float(accuracy[j]) / float(i + 1) if args.top_k[j] == 1: print '\ttop_' + str(args.top_k[j]) + ':' + str(tmp_acc), else: print 'top_' + str(args.top_k[j]) + ':' + str(tmp_acc) end_time = datetime.datetime.now() w = open(LOG_PTH, 'w') s1 = 'Evaluation process ends at: {}. \nTime cost is: {}. '.format(str(end_time), str(end_time - start_time)) s2 = '\nThe model is: {}. \nThe val file is: {}. \n{} images has been tested, crop_type is: {}, base_size is: {}, ' \ 'crop_size is: {}.'.format(args.model_weights, args.val_file, str(eval_len - args.skip_num), args.crop_type, str(args.base_size), str(args.crop_size)) s3 = '\nThe PIXEL_MEANS is: ({}, {}, {}), PIXEL_STDS is : ({}, {}, {}).' \ .format(str(PIXEL_MEANS[0]), str(PIXEL_MEANS[1]), str(PIXEL_MEANS[2]), str(PIXEL_STDS[0]), str(PIXEL_STDS[1]), str(PIXEL_STDS[2])) s4 = '' for i in xrange(len(args.top_k)): _acc = float(accuracy[i]) / float(eval_len - args.skip_num) s4 += '\nAccuracy of top_{} is: {}; correct num is {}.'.format(str(args.top_k[i]), str(_acc), str(int(accuracy[i]))) print s1, s2, s3, s4 w.write(s1 + s2 + s3 + s4) w.close() if args.save_score_vec: w = open(LOG_PTH.replace('.txt', 'scorevec.txt'), 'w') for i in xrange(eval_len - args.skip_num): w.write(SET_DICT[i + args.skip_num]['score_vec']) w.close() print('DONE!')