def lv1_user_function_sampling_and_predict_meshgrid_rectangular_and_edge( n_samples, target, clone_model, grid_n_size, edge_distance): # grid_n_size = 500 if n_samples <= grid_n_size: return lv1_user_function_sampling_meshgrid_rectangular( n_samples=n_samples) else: grid_features = lv1_user_function_sampling_meshgrid_rectangular( n_samples=grid_n_size) grid_labels = target.predict(features=grid_features) clone_model.fit(grid_features, grid_labels) # 学習したクローン認識器を可視化し,精度を評価 evaluator = LV1_Evaluator() clone_img = evaluator.visualize_get_img(clone_model) edge_img = filter_edge(img=clone_img) edge_features = evaluator.edge_img_to_edge_features( edge_img=edge_img, edge_distance=edge_distance) print('edge_features size: ' + str(len(edge_features))) print('grid shape' + str(grid_features.shape)) print('edge shape' + str(edge_features.shape)) return np.vstack((grid_features, edge_features))
def exe_my_clone(target, img_save_path, missing_img_save_path, n, method_name): # , grid_n_size, edge_distance): # ターゲット認識器への入力として用いる二次元特徴量を用意 features = get_features(n, method_name) print(features) print(features.shape) print(features[0]) # print("\n{0} features were sampled.".format(n)) # ターゲット認識器に用意した入力特徴量を入力し,各々に対応するクラスラベルIDを取得 labels = target.predict(features) print("\nThe sampled features were recognized by the target recognizer.") # クローン認識器を学習 model = LV1_UserDefinedClassifier() model.fit(features, labels) print("\nA clone recognizer was trained.") # 学習したクローン認識器を可視化し,精度を評価 evaluator = LV1_Evaluator() evaluator.visualize(model, img_save_path) print('visualized') evaluator.visualize_missing(model=model, target=target, filename=missing_img_save_path, features=features) print("\nThe clone recognizer was visualized and saved to {0} .".format( img_save_path)) accuracy = evaluator.calc_accuracy(target, model) sampling_accuracy = evaluator.calc_sampling_accuracy( target=target, model=model, sampling_features=features) print("\naccuracy: {0}".format(accuracy)) print("\naccuracy in sampling: {0}".format(sampling_accuracy)) return accuracy, sampling_accuracy
def exe_clone(target, exe_n, method_name, path_manager: SavePathManager): # ターゲット認識器への入力として用いる二次元特徴量を用意 features = get_features(target=target, exe_n=exe_n, method_name=method_name) print(features) print(features.shape) print(features[0]) # print("\n{0} features were sampled.".format(exe_n)) # クローン認識器を学習 labels = target.predict(features) # model = LV1UserDefinedClassifierRandomForest() model = LV1UserDefinedClassifier1NNRetry() model.fit(features, labels) print("\nA clone recognizer was trained.") # 学習したクローン認識器を可視化し,精度を評価 evaluator = LV1_Evaluator() visualize_save_dir = path_manager.sampling_method_dir( exe_n=exe_n, method_name=method_name) create_dir(visualize_save_dir) # evaluator.visualize(model, os.path.join(visualize_save_dir, 'visualize.png')) print('visualized') evaluator.visualize_missing(model=model, target=target, filename=os.path.join(visualize_save_dir, 'visualize_miss.png'), features=features) print("\nThe clone recognizer was visualized and saved to {0} .".format( visualize_save_dir)) accuracy = evaluator.calc_accuracy(target, model) print("\naccuracy: {0}".format(accuracy)) return accuracy
def main(): if len(sys.argv) < 3: print( "usage: clone.py /target/classifier/image/path /output/image/path") exit(0) # ターゲット認識器を用意 target = LV1_TargetClassifier() target.load(sys.argv[1]) # 第一引数で指定された画像をターゲット認識器としてロード print("\nA target recognizer was loaded from {0} .".format(sys.argv[1])) # ターゲット認識器への入力として用いる二次元特徴量を用意 # このサンプルコードではひとまず100サンプルを用意することにする n = 100 features = lv1_user_function_sampling_meshgrid(n_samples=n) print(features) print(features.shape) print(features[0]) print("\n{0} features were sampled.".format(n)) # ターゲット認識器に用意した入力特徴量を入力し,各々に対応するクラスラベルIDを取得 labels = target.predict(features) print("\nThe sampled features were recognized by the target recognizer.") # クローン認識器を学習 model = LV1_UserDefinedClassifier() model.fit(features, labels) print("\nA clone recognizer was trained.") # 学習したクローン認識器を可視化し,精度を評価 evaluator = LV1_Evaluator() evaluator.visualize(model, sys.argv[2]) print("\nThe clone recognizer was visualized and saved to {0} .".format( sys.argv[2])) print("\naccuracy: {0}".format(evaluator.calc_accuracy(target, model)))
target = LV1_TargetClassifier() target.load(sys.argv[1]) # 第一引数で指定された画像をターゲット認識器としてロード print("\nA target recognizer was loaded from {0} .".format(sys.argv[1])) # ターゲット認識器への入力として用いる二次元特徴量を用意 # このサンプルコードではひとまず100サンプルを用意することにする n = 10000 features = lv1_user_function_sampling_sweeper(n_samples=n, exe_n=n, target_model=target) # features = lv1_user_function_sampling_meshgrid_rectangular(n_samples=n) print("\n{0} features were sampled.".format(n)) # ターゲット認識器に用意した入力特徴量を入力し,各々に対応するクラスラベルIDを取得 labels = target.predict(features) print("\nThe sampled features were recognized by the target recognizer.") # クローン認識器を学習 model = LV1UserDefinedClassifierSVM() model.fit(features, labels) print("\nA clone recognizer was trained.") # 学習したクローン認識器を可視化し,精度を評価 evaluator = LV1_Evaluator() evaluator.visualize(model, sys.argv[2]) print("\nThe clone recognizer was visualized and saved to {0} .".format( sys.argv[2])) print("\naccuracy: {0}".format(evaluator.calc_accuracy(target, model)))
def main(): ''' if len(sys.argv) < 3: print("usage: clone.py /target/classifier/image/path /output/image/path") exit(0) ''' # このプログラムファイルの名前と同じdirectoryを作り、その中に結果を保存する。 now = datetime.now().strftime('%Y%m%d%H%M%S') repo = git.Repo(search_parent_directories=True) sha = repo.head.object.hexsha commit_hash = repo.git.rev_parse(sha) output_root_path = './output/area_' + now + '_' + str(commit_hash) create_dir(output_root_path) democracy = 'democracy' method_names = [democracy] svm = 'svm' knn = 'knn' mlp = 'mlp' random_forest = 'random_forest' decision_tree = 'desicion_tree' model_names = [svm, knn, random_forest, mlp, decision_tree] for max_value in range(100, 1000, 300): for method_name in method_names: for model_name in model_names: # directory_name = input('作成するdirectoryを入力してください>>>>') directory_name = method_name + '_' + model_name + '_max' + str( max_value) directory_path = LV1_user_make_directory( output_root_path, directory_name) # print(directory_path) # Lv1_targetsに存在する画像ファイル名を取得する。 # path = './lv1_targets' path = './lv1_my_targets_from_img7' target_image = LV1_user_load_directory(path) # print(target_image) target_image.sort() print(target_image) # ターゲット認識器を用意 target = LV1_TargetClassifier() # 面積のpixel数を格納するlist area_pixel = [] last_size = 0 target_names = [] # target.load(load_path)をLv1_targetsに含まれる画像毎に指定する。 for i in target_image: # 全部のtargetsをやりたくないときはここをいじって。 # if i.split('/')[-1].replace('.png','') == 'classifier_03':break target_names.append(os.path.basename(i).split('_')[0]) target.load(i) # 入力したdirectoryにtarget_image毎のdirectoryを作成する。 target_directory = LV1_user_make_directory( directory_path, i.split('/')[-1].replace('.png', '')) # print(target_directory) # 学習したクローン認識器を可視化した画像を保存するためのファイルを作成。 clone_image_directory = LV1_user_make_directory( target_directory, 'clone_image') # print(clone_image_directory) # accuracyの結果を保存するフォルダを作成する。 accuracy_directory = LV1_user_make_directory( target_directory, 'accuracy_area') # print(accuracy_directory) # ターゲット認識器への入力として用いる二次元特徴量を用意 # このサンプルコードではひとまず1000サンプルを用意することにする # Nごとのaccuracyを格納する配列を用意する。面積を計算するため。 accuracy_list = [] # Nにサンプル数の配列を指定する。 # ↓N=[1,100]は実行時間を短くしたために書いてます。 # ↓間隔を自分で試したい場合はいじってください。下記の[1~10000]の配列を使う場合はコメントして。 # N = [1,100] # ↓[1,100,200,・・・,10000]までを100間隔でおいた配列がコメントを外すと生成されます。 # N1 = np.array([1]) # N2 = np.arange(100, 1001, 100) # N = np.hstack((N1, N2)) # N1 = np.arange(1, 10, 1) # N2 = np.arange(10, 100, 10) # N3 = np.arange(100, 1001, 100) # N = np.hstack((N1, N2)) # N = np.hstack((N, N3)) n1 = np.array([1]) n2 = np.arange(10, max_value + 1, int(max_value / 10)) N = np.hstack((n1, n2)) print(N) for n in N: start = time.time() board_size = math.ceil(math.sqrt(n)) + 2 if method_name == democracy: features = lv1_user_function_sampling_democracy( n_samples=n, target_model=target, exe_n=n) else: raise ValueError # ターゲット認識器に用意した入力特徴量を入力し,各々に対応するクラスラベルIDを取得 labels = target.predict(features) # クローン認識器を学習 if model_name == svm: model = LV1UserDefinedClassifierSVM10C10Gamma() elif model_name == knn: model = LV1UserDefinedClassifier1NN() elif model_name == decision_tree: model = LV1UserDefinedClassifierTree1000MaxDepth() elif model_name == random_forest: model = LV1UserDefinedClassifierRandomForest() elif model_name == mlp: model = LV1UserDefinedClassifierMLP1000HiddenLayer( ) else: raise ValueError model.fit(features, labels) # 学習したクローン認識器を可視化し,精度を評価 evaluator = LV1_Evaluator() # 可視化した画像を保存。 output_path = clone_image_directory + '/' + directory_name + '_[output_(' + str( n) + ')].png' evaluator.visualize_missing(model=model, filename=output_path, features=features, target=target) # accuracyを配列に格納。 accuracy = evaluator.calc_accuracy(target, model) accuracy_list.append(accuracy) end = time.time() print('終了:', i.split('/')[2].replace('.png', ''), '_(', n, ')[', round((end - start), 2), '(sec)]') # accuracyの面積グラフを作成して保存 area_path = accuracy_directory + '/' + directory_name + '_(accuracy_area).png' area_features = LV1_user_accuracy_plot( accuracy_list, N, area_path) # 面積のグラフをcutする。 cut_path = area_path.replace('.png', '_cut.png') area_cut = LV1_user_plot_cut(area_path, cut_path) # accuracyのぶりつぶされたpixelを数える。 count_path = area_path.replace('.png', '_count.png') pixel_count, area_size = LV1_user_area_pixel_count( cut_path, count_path) area_pixel.append(pixel_count) # accuracyの面積結果を画像で保存する。 text_path = area_path.replace('.png', '_text.png') area_text = LV1_user_area_count_text( text_path, pixel_count, area_size) last_size = area_size print('画像サイズ[', area_size, ']_x[', area_size[0], ']_y[', area_size[1], ']') print( '面積pixel[', pixel_count, ']_割合[', round( pixel_count / (area_size[0] * area_size[1]) * 100, 2), '%]') statistics_path = directory_path + '/' + directory_name + '_(statistics).png' LV1_user_area_statistics(statistics_path, area_pixel, target_names, last_size, title=model_name)