def convert_to_position_and_rowcol_img(img_path, output_position_path, output_img_path, filter_last=False): img = cv2.imread(img_path, 0) detectionList, rolColImg = convert2RowCol(img, filter_last) yolo_position_list = to_yolo_position_from_2dim_list(img.shape[1], img.shape[0], detectionList) write_file(yolo_position_list, output_position_path, 2 ) cv2.imwrite(output_img_path, rolColImg ) return rolColImg, yolo_position_list
def attribute_classification_predit(encoder_model: Model, decoder_model: Model, input_image_path, input_shape, decoder_token_list, max_decoder_seq_length, result_saved_path=None, img_input_type='path', keep_ratio=True): img_data = preprocess_image(input_image_path, input_shape, img_input_type=img_input_type, keep_ratio=keep_ratio) w, h, c = img_data.shape img_input = np.zeros((1, w, h, c), dtype='float32') img_input[0] = img_data states_value = encoder_model.predict(img_input) tokens_dict = decoder_tokens_list_to_dict(decoder_token_list) target_seq = np.zeros((1, 1, len(tokens_dict))) target_seq[0, 0, tokens_dict['START']] = 1. reverse_tokens_dict = dict((i, token) for token, i in tokens_dict.items()) stop_condition = False decoded_sentence = [] while not stop_condition: output_tokens, h, c = decoder_model.predict([target_seq] + states_value) # Sample a token sampled_token_index = np.argmax(output_tokens[0, -1, :]) sampled_token = reverse_tokens_dict[sampled_token_index] print('sampled_token:', sampled_token_index, sampled_token) if sampled_token != 'EOS': decoded_sentence.append(sampled_token) # Exit condition: either hit max length # or find stop character. if (sampled_token == 'EOS' or len(decoded_sentence) > max_decoder_seq_length): stop_condition = True # Update the target sequence (of length 1). target_seq = np.zeros((1, 1, len(tokens_dict))) target_seq[0, 0, sampled_token_index] = 1. # Update states states_value = [h, c] if result_saved_path: write_file(decoded_sentence, result_saved_path, dataDim=1) return decoded_sentence
def create_attribute_classfication_dataset(attr_positions_folder, image_folder, element_folder, target_path, record_path, label_list, element_start_index, file_start_index=0, file_num=1, balance=True, initial_each_element=[0, 0, 0], proportion=[1, 1, 1]): element_index = element_start_index num_each_element = initial_each_element prop_each_element = proportion with open(target_path, 'a+') as f: for file_idx in range(file_start_index, file_start_index + file_num): img = cv2.imread(image_folder + str(file_idx) + TYPE.IMG) read_positions = read_file( attr_positions_folder + str(file_idx) + TYPE.TXT, 'splitlines') positions = [position.split() for position in read_positions] for position in positions: if balance: t = int(position[0]) if sum(prop_each_element) > 0 and ( num_each_element[t] / (element_index + 1) > prop_each_element[t] / (sum(prop_each_element))): continue else: num_each_element[t] += 1 sub_img = splitImage(img, position) attributes = position[5:] element_file_name = element_folder + str( element_index) + TYPE.IMG f.write('{} {}\n'.format( element_file_name, ' '.join([str(a) for a in attributes]))) cv2.imwrite(element_file_name, sub_img) element_index += 1 print(file_idx) if file_idx % 10 == 0 else None record = 'number of used file: {}\nnumber of total_elements: {}\nnumber of type 0 [Title]: {}\nnumber of type 1 [Text]: {}\nnumber of type 2 [Btn]: {}\nnumber of type 3 [Text_input]: {}\nprop: {}'.format( file_start_index + file_num, element_index, num_each_element[0], num_each_element[1], num_each_element[2], num_each_element[3], prop_each_element) write_file(record, record_path, 0) return element_index
def assign_tag(self, tag): self.class_position.append([tag] + self.positions[self.nowPositionIndex][1:]) if self.nowPositionIndex + 1 < self.len_position: self.nowPositionIndex += 1 self.now_position = tk.Label(self, text='{}/{}'.format( self.nowPositionIndex, self.len_position), font=25).grid(row=0, column=1, sticky=tk.W) self.changeImage() else: showinfo("(*´Д`)つ))´∀`)", "心趙不宣,磊落不凡") write_file(self.class_position, self.output_position_path, 2) self.destroy()
def attribute_classfication_training(train_model: Model, encoder_config, decoder_config, checkpoint_folder, analysis_saved_folder, final_model_saved_path, initial_epoch=0, keep_ratio=True): createFolder(checkpoint_folder + str(EPOCHES)) mc = callbacks.ModelCheckpoint(checkpoint_folder + str(EPOCHES) + '\\attr-classfy-weights{epoch:05d}.h5', save_weights_only=True, period=MODE_SAVE_PERIOD) early_stopping = callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1) lines = read_file(decoder_config['data_path'], 'splitlines') num_train = encoder_config['num_train'] num_valid = encoder_config['num_valid'] token_list = decoder_config['token_list'] input_shape = encoder_config['input_shape'] # print('-----config----- \nnum_train: {}\nnum_valid: {}\ninput_shape: {}\nsteps_per_epoch: {}\n'.format(num_train, num_valid, input_shape, max(1, num_train//BATCH_SIZE))) history = train_model.fit_generator( attributes_data_generator(lines[:num_train], BATCH_SIZE, input_shape, token_list, keep_ratio=keep_ratio), steps_per_epoch=max(1, num_train // BATCH_SIZE), validation_data=attributes_data_generator( lines[num_train:num_train + num_valid], BATCH_SIZE, input_shape, token_list), validation_steps=max(1, num_valid // BATCH_SIZE), epochs=EPOCHES, initial_epoch=initial_epoch, callbacks=[mc, early_stopping]) showLoss(history, analysis_saved_folder, 'loss' + str(EPOCHES)) showAccuracy(history, analysis_saved_folder, 'accuracy' + str(EPOCHES)) write_file(history.history, analysis_saved_folder + 'history' + str(EPOCHES) + TYPE.TXT, 'JSON') train_model.save(final_model_saved_path) return train_model
def yolo_position_with_noise_generator(yolo_position_folder, gui_folder=None, new_positions_folder=None, new_gui_folder=None, data_num=500, multiple=5, resort=False, save_origin_file=True): createFolder(new_positions_folder) if new_gui_folder: createFolder(new_gui_folder) for i in range(data_num): if gui_folder: read_gui = read_file(gui_folder + str(i) + TYPE.GUI, 'noSplit') read_positions = read_file(yolo_position_folder + str(i) + TYPE.TXT, 'splitlines') positions = [position.split() for position in read_positions] print(positions) if save_origin_file: if gui_folder: write_file(read_gui, new_gui_folder + str(i) + TYPE.GUI, 0) write_file(positions, new_positions_folder + str(i) + TYPE.TXT, 2) positions = np.array(positions) positions = positions.astype(np.float) for times in range(1 if save_origin_file else 0, multiple): if gui_folder: new_gui_file_name = new_gui_folder + str(data_num * times + i) + TYPE.GUI new_position_file_name = new_positions_folder + \ str(data_num*times+i)+TYPE.TXT new_positions = positions.copy() if new_positions.shape[0] > 0: new_positions[:, 1:] = new_positions[:, 1:] + np.random.normal( 0, 0.0025, (new_positions.shape[0], new_positions.shape[1] - 1)) new_positions[:, 1:] = new_positions[:, 1:].clip(0, 1) new_positions = new_positions.tolist() for position in new_positions: position[0] = int(position[0]) if resort: new_positions.sort(key=lambda x: x[1]) new_positions.sort(key=lambda x: x[2]) if gui_folder: write_file(read_gui, new_gui_file_name, 0) write_file(new_positions, new_position_file_name, 2) print(i) if i % 500 == 0 else None
input_shape = encoder_config['input_shape'] str_training_data = '\n\ntraining data: \n from: {}\n to: {}'.format( 0, test_start) print(str_training_data) str_train_result = attribute_classification_evaluate( load_model(evaluate_model_path), 0, test_start, input_shape, decoder_config) str_testing_data = '\n\ntesting data: \n from: {}\n to: {}'.format( test_start, test_end) print(str_testing_data) str_test_result = attribute_classification_evaluate( load_model(evaluate_model_path), test_start, test_end, input_shape, decoder_config) res = str_model_path + str_data_file + str_training_data + str_train_result + str_testing_data + str_test_result createFolder(eva_record_path) write_file(res, eva_record_path + eva_record_name, dataDim=0) if PREDIT: # createFolder(path.CLASS_ATTR_PREDIT_GUI_PATH + str(EPOCHES)) encoder_model, decoder_model = attribute_classification_predit_model( load_model(predit_model_path)) max_data_length = len(lines) predit_list = [] for i in range(predit_data_num): # idx = random.randint(0, max_data_length+1) # print('predit_GT: ', lines[idx]) line = lines[i].split() print('origin:', line) decoded_sentence = attribute_classification_predit( encoder_model, decoder_model, line[0], encoder_config['input_shape'], decoder_config['token_list'], 4)
except: print('Open Error! Try again!') else: r_image, r_targets = yolo.detect_image(image, output_score=True) # r_image.show() # yolo.close_session() return r_targets if __name__ == "__main__": yolo_model_name = 'pix2code_full\\trained_weights_final(011).h5' yolo_classes_name = 'pix2code_full_classes.txt' yolo_anchors_name = 'yolo_anchors.txt' input_image_folder = path.DATASET1_ORIGIN_PNG detected_save_folder = path.YOLO_DETECTED_DATA1_FULL_POSITION_TXT + 'Arch1_test\\011\\' # detected_save_folder = path.YOLO_DETECTED_FULL_POSITION_TXT createFolder(detected_save_folder) yolo_class = YOLO(model_name=yolo_model_name, classes_name=yolo_classes_name, anchors_name=yolo_anchors_name) # yolo_class = YOLO() for i in range(100): targets = detect_img(yolo_class, input_image_folder + str(500 + i) + TYPE.IMG) print(i) if i % 10 == 0 else None write_file(targets, detected_save_folder + str(i) + TYPE.TXT, dataDim=2)
print(str_training) encoder_input_data, decoder_input_data, decoder_target_tokens, max_decoder_len = to_Seq2Seq_input( encoder_config['data_folder'], decoder_config['data_folder'], encoder_config, decoder_config['token_list'], data_num=evaluate_data_num) str_training_result = seq2seq_evaluate(load_model(evaluate_model_path), encoder_input_data, decoder_input_data, decoder_target_tokens) str_testing = '\n\ntesting data path: \n encoder: {}\n decoder: {}'.format( encoder_config['testing_data_folder'], decoder_config['testing_data_folder']) print(str_testing) encoder_input_data, decoder_input_data, decoder_target_tokens, max_decoder_len = to_Seq2Seq_input( encoder_config['testing_data_folder'], decoder_config['testing_data_folder'], encoder_config, decoder_config['token_list']) str_testing_result = seq2seq_evaluate(load_model(evaluate_model_path), encoder_input_data, decoder_input_data, decoder_target_tokens) evaluate_save_text = str_model_path+str_training+str_training_result+str_testing+str_testing_result write_file(evaluate_save_text, dataDim=0) if PREDIT: createFolder(path.CLASS_SEQ2SEQ_PREDIT_GUI_PATH + str(SEQ2SEQ_EPOCHES)) decoder_target_tokens = decoder_tokens_list_to_dict(decoder_config['token_list']) max_decoder_len = 300 encoder_model, decoder_model = seq2seq_predit_model( load_model(predit_model_path), model_type=seq_model_type, layer2_lstm=layer2_lstm) # data_folder = 'testing_data_folder' if predit_test_data else 'data_folder' for data_folder, predit_data_num in zip(['data_folder', 'testing_data_folder'], predit_data_nums): valid_data_num = predit_data_num if BLEU_SCORE: bleu = Bleu(predit_data_num, 0, encoder_config[data_folder], decoder_config[data_folder], predit_model_path) if ERROR_SCORE: