def load_image(imgdir, size, norm=True, median=True, median_size=3, mean=True, consider=True): """ input: image directory output: numpy array """ images = [] imglist = os.listdir(imgdir) imglist.sort() for filename in imglist: if not filename.startswith('.'): if len(filename) > 10: img = Image.open(os.path.join(imgdir, filename)) if img is not None: img = img.resize(size) img = np.array(img, dtype=np.float32) if consider is True: img = make_considered_picture(img=img, level=4) if norm is True: img = img / 255. if median is True: img = ndimage.median_filter(img, median_size) images.append(img) img_array = np.array(images, dtype=np.float32) print("---------- Image Load Done") return img_array
def main(): # 画像の読み込み if args.mnist: (x_train, y_train), (x_test, y_test) = mnist.load_data() else: x_data = [] x_list = glob(os.path.join(data_path, "*.png")) for path in tqdm(ls): img = Image.open(path) img = img.resize((100, 100)) img = np.array(img, dtype=np.float32) x_data.append(img) x_data = np.array(x_data) img_ori = np.zeros((224, 224, 3)) level = 4 img_fish, pos = fish.make_considered_picture(img=img_ori, level=level, return_array=1) pos_x = rotation_xyz(pos, alpha=np.pi/2, mode="x") print("level: ", level) print(img_fish.shape)
# from keras.utils import np_utils from tqdm import tqdm from PIL import Image (x_train, t_train), (x_test, t_test) = cifar100.load_data() # load sphere array # x_train, t_train = x_train[:50], t_train[:50000] # x_test, t_test = x_test[:10], t_test[:10000] level = 6 hemisphere = 60 r = 1 Q = 2**level img_ori = np.zeros((224, 224, 3)) img_fish, pos = fish.make_considered_picture(img=img_ori, level=level, return_array=1) # process equi image x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train, x_train_sp = it.make_sphere_batch(train_generator=x_train, pos=pos, start='random', hemisphere=hemisphere, r=r, return_equ=False) x_test, x_test_sp = it.make_sphere_batch(train_generator=x_test, pos=pos, start='random', hemisphere=hemisphere,
def main(args): data_dir, save_dir, mode = args.data_dir, args.save_dir, args.mode # pos img_ori = np.zeros((224, 224, 3)) level = 4 img_fish, pos = fish.make_considered_picture(img=img_ori, level=level, return_array=1) print("level: ", level) print(img_fish.shape) # data load ndar = np.load(data_dir) train = ndar['train'] test = ndar['test'] x_train, y_train = make_data(train) x_test, y_test = make_data(test) x_train = get_sphere_array(x_train, pos) x_test = get_sphere_array(x_test, pos) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 if x_train.shape[-1] == 3: print('change channel') x_train = x_train[:, :, :, 0] x_test = x_test[:, :, :, 0] # if len(x_test) > 1000: # tmp = 1000 # x_train = np.concatenate((x_train, x_test[tmp:]), axis=0) # x_test = x_test[:tmp] # y_train = np.concatenate((y_train, y_test[tmp:]), axis=0) # y_test = y_test[:tmp] x_test = x_test[:1000] y_test = y_test[:1000] # パラメータの設定 hemisphere = 50 Q = 2**level title = 'SphereConv' batch_size = 100 max_epochs = 10 out_num = 10 print('conducting in SphereConv') # パラメータの吐き出し with open(save_dir + 'setting.txt', 'w') as f: f.write('save_dir: ' + save_dir + '\n') f.write('hemisphere: ' + str(hemisphere) + '\n') f.write('level: ' + str(level) + '\n') f.write('Q: ' + str(Q) + '\n') f.write('batch_size: ' + str(batch_size) + '\n') f.write('max_epochs: ' + str(max_epochs) + '\n') f.write('out_num :' + str(out_num) + '\n') f.write('train : 10000' + '\n') f.write('test 1000 :' + '\n') f.write('COMMENT: SGD, SphereConv\n') print('train size;', x_train.shape) network = SphereConv(input_dim=(3, 6, Q + 2, 2 * Q + 2), conv_param={ 'filter_num': 32, 'filter_size': 7, 'pad': 0, 'stride': 1 }, hidden_size=128, output_size=out_num, weight_init_std='he', level=level) trainer = Trainer(network, x_train, y_train, x_test[:100], y_test[:100], epochs=max_epochs, mini_batch_size=batch_size, optimizer='SGD', optimizer_param={'lr': 0.001}, evaluate_sample_num_per_epoch=100) trainer.train() # パラメータの保存 network.save_params(save_dir + "params.pkl") print("Saved Network Parameters!") try: print('save acc') a = np.array(trainer.train_acc_list) b = np.array(train_acc_list.test_acc_list) np.savez('acc.npz', train=a, test=b) except: print('error in saving acc') # グラフの描画 markers = {'train': 'o', 'test': 's'} x = np.arange(max_epochs) plt.plot(x, trainer.train_acc_list, marker='o', label='train', markevery=2) plt.plot(x, trainer.test_acc_list, marker='s', label='test', markevery=2) plt.xlabel("epochs") plt.ylabel("accuracy") plt.ylim(0, 1.0) plt.legend(loc='lower right') plt.savefig(save_dir + "test.png")
def main(args): data_dir, save_dir = args.data_dir, args.save_dir (x_train, y_train), (x_test, y_test) = mnist.load_data() # y_test = np_utils.to_categorical(y_test) # y_train = np_utils.to_categorical(y_train) img_ori = np.zeros((224, 224, 3)) level = 4 img_fish, pos = fish.make_considered_picture(img=img_ori, level=level, return_array=1) pos_x = rotation_xyz(pos, alpha=np.pi/2, mode="x") print("level: ", level) print(img_fish.shape) print('making train samples') # x_train, y_train = make_spherical_mnist(x=x_train[:10000], y=y_train[:10000]) # output_train = make_spherical_mnist(x=x_train[:30000], y=y_train[:30000]) output_train = make_spherical_mnist(x=x_train[:10000], y=y_train[:10000], pos=pos_x) print("arraying") output_train = np.array(output_train) print('making test samples') # x_test, y_test = make_spherical_mnist(x=x_test[:1000], y=y_test[:1000]) output_test = make_spherical_mnist(x=x_test, y=y_test, pos=pos_x) output_test = np.array(output_test) title = 'sphere_mnist_pica_level4_cluster7x_10000.npz' print(title) path_sp = os.path.join(save_dir, title) # np.save(path_sp, output) np.savez(path_sp, train=output_train, test=output_test, ) pos_xz = rotation_xyz(pos, alpha=np.pi/2, mode="xz") print('making train samples') output_train = make_spherical_mnist(x=x_train[:10000], y=y_train[:10000], pos=pos_xz) print("arraying") output_train = np.array(output_train) print('making test samples') output_test = make_spherical_mnist(x=x_test, y=y_test, pos=pos_xz) output_test = np.array(output_test) title = 'sphere_mnist_pica_level4_cluster7xz_10000.npz' # title = 'sphere_mnist_unique_v1_cluster5_10000.npz' print(title) path_sp = os.path.join(save_dir, title) # np.save(path_sp, output) np.savez(path_sp, train=output_train, test=output_test, ) print('making train samples') output_train = make_spherical_mnist(x=x_train[:10000], y=y_train[:10000]) print("arraying") output_train = np.array(output_train) print('making test samples') output_test = make_spherical_mnist(x=x_test, y=y_test) output_test = np.array(output_test) title = 'sphere_mnist_unique_v1_cluster7_10000.npz' print(title) path_sp = os.path.join(save_dir, title) # np.save(path_sp, output) np.savez(path_sp, train=output_train, test=output_test, )
def main(args): ####################### # ディレクトリパスの読み込み ####################### # 今日の日付 today_time = dt.datetime.today().strftime("%Y_%m_%d") # 各ディレクトリの設定 DATA_DIR, SAVE_DIR = \ args.data_dir, args.save_dir # 'SAVE_DIR'に今日の日付を足す do_time = os.datetime.today().strftime("%H_%M_%S") SAVE_DIR = os.path.join(SAVE_DIR, today_time, do_time) if not os.path.isdir(SAVE_DIR): os.makedirs(SAVE_DIR) ####################### # Hyper Parameter ####################### param_dic = {} config = configparser.ConfigParser() config.read('config.ini') param_sphere = config['sphere'] level = param_dic['level'] = int(param_sphere['level']) hemisphere = param_dic['hemisphere'] int(param_sphere['hemisphere']) Q = param_dic['Q'] = 2**level title = param_dic['title'] = param_sphere['title'] param = config['parameter'] batch_size = param_dic['batch_size'] = param['batch_size'] max_epochs = param_dic['max_epochs'] = param['max_epochs'] evaluate_num = param_dic['evaluate_sample_num_per_epoch'] = param['evaluate_num'] out_num = param_dic['out_num'] = param['out_num'] filter_num = param_dic['filter_num'] = param['filter_num'] filter_size = param_dic['filter_size'] = param['filter_size'] padding = param_dic['padding'] = param['padding'] stride = param_dic['stride'] = param['stride'] hidden_size = param_dic['hidden_size'] = param['hidden_size'] optimizer = param_dic['optimizer'] = param['optimizer'] lr = param_dic['lr'] = param['optimizer'] weight_init_std = param_dic['weight_init_std'] = param['weight_init_std'] # 保存 with open(SAVE_DIR + 'setting.txt', 'a') as f: for key, value in param_dic.items(): f.write(str(key) + ':' + str(value)) f.write('\n') ####################### # 球面座標の獲得 ####################### img_ori = np.zeros((224, 224, 3)) img_fish, pos = fish.make_considered_picture(img=img_ori, level=level, return_array=1) # print("level: ", level) # print(img_fish.shape) ####################### # Load Data ####################### ndar = np.load(data_dir) train = ndar['train'] test = ndar['test'] ####################### # Make data # データをネットワークに適した形に変形する ####################### x_train, y_train = make_data(train) x_test, y_test = make_data(test) ####################### # 球面画像の獲得 ####################### x_train = get_sphere_array(x_train, pos) x_test = get_sphere_array(x_test, pos) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 if x_train.shape[-1] == 3: print('change channel') x_train = x_train[:, :, :, 0] x_test = x_test[:, :, :, 0] # if len(x_test) > 1000: # tmp = 1000 # x_train = np.concatenate((x_train, x_test[tmp:]), axis=0) # x_test = x_test[:tmp] # y_train = np.concatenate((y_train, y_test[tmp:]), axis=0) # y_test = y_test[:tmp] x_test = x_test[:1000] y_test = y_test[:1000] ####################### # Train ####################### network = SphereConv(input_dim=(3, 6, Q+2, 2*Q+2), conv_param={'filter_num': filter_num, 'filter_size': filter_size, 'pad': padding, 'stride': stride}, hidden_size=hidden_size, output_size=out_num, weight_init_std=weight_init_std, level=level) trainer = Trainer(network, x_train, y_train, x_test[:100], y_test[:100], epochs=max_epochs, mini_batch_size=batch_size, optimizer=optimizer, optimizer_param={'lr': lr}, evaluate_sample_num_per_epoch=evaluate_num) trainer.train() ####################### # パラメータの保存 ####################### network.save_params(save_dir+"params.pkl") print("Saved Network Parameters!") try: print('save acc') a = np.array(trainer.train_acc_list) b = np.array(train_acc_list.test_acc_list) np.savez('acc.npz', train=a, test=b) except: print('error in saving acc') # グラフの描画 markers = {'train': 'o', 'test': 's'} x = np.arange(max_epochs) plt.plot(x, trainer.train_acc_list, marker='o', label='train', markevery=2) plt.plot(x, trainer.test_acc_list, marker='s', label='test', markevery=2) plt.xlabel("epochs") plt.ylabel("accuracy") plt.ylim(0, 1.0) plt.legend(loc='lower right') plt.savefig(save_dir+"test.png")