def main(): cats, X, y = get_data.read_data() train(cats, X, y, f_maps=128, conv_stride=[5, 5], maxp_stride=[2, 2], training_epochs=training_epochs)
def finish(self): global lastMode global lastPID lastMode = 0 data = get_data.read_data() print("data[0]:", len(data[0])) if data[0] != "K": os.kill(lastPID, signal.SIGKILL) lastPID = None get_data.write_data(str("K"), str(lastMode)) print("disconnected\tIP:" + self.ip + "\tPort:" + str(self.port))
def process(filename): quotes = read_data(filename) print("Length of quotes", len(quotes)) #quotes appended to a single string. all_quotes = "" for q in quotes: all_quotes += q + "\n" #Map each character to an integer chars = tuple(set(all_quotes)) int2char = dict(enumerate(chars)) char2int = {ch: ii for ii, ch in int2char.items()} # Encode the text encoded = np.array([char2int[ch] for ch in all_quotes]) return chars, encoded
def handle(self): global lastMode global lastPID Flag = True while Flag: try: recv = self.request.recv(128) # 接收数据v if recv == b'': Flag = False # 空则推出 else: self.buf += recv.decode() # 解码 #print(self.buf) self.buf = re.sub(r'333333', '', self.buf, 10) # 约定客户端通过发送'3'来进行心跳, 删除字符串中的3 s = re.search(r'mode=\d{1,2}', self.buf, re.I) # 查找字符串中的 MODE=数字 格式的字串 if s: self.buf = "" # 只要找到一个就将所有的缓存清除 Mode = int(s.group()[5:]) # 从字串中获取mode的数值 print(Mode) data = get_data.read_data() if data[1] == "0": lastMode = 0 # 根据Mode的数值 向 对于的子进程发送继续运行信号,对应的子进程运行,产生效果 if Mode == 0: lastMode = Mode try: os.kill(lastPID, signal.SIGKILL) except: pass self.request.sendall("OK".encode()) # 向客户端发送“OK" elif Mode == 1: if lastMode != Mode: lastMode = Mode if lastPID is not None and get_data.read_data( )[0] != 'K': os.kill(lastPID, signal.SIGKILL) ChildCvFindStream = subprocess.Popen([ "python3", "/home/pi/human_code/cv_find_stream.py" ]) # 自动射门 lastPID = ChildCvFindStream.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 2: if lastMode != Mode: lastMode = Mode if lastPID is not None and get_data.read_data( )[0] != 'K': os.kill(lastPID, signal.SIGKILL) ChildColorStream = subprocess.Popen([ "python3", "/home/pi/human_code/cv_color_stream.py" ]) # 颜色识别 lastPID = ChildColorStream.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 3: if lastMode != Mode: lastMode = Mode if lastPID is not None and get_data.read_data( )[0] != 'K': os.kill(lastPID, signal.SIGKILL) ChildCvFindHand = subprocess.Popen([ "python3", "/home/pi/human_code/cv_find_hand.py" ]) # 手势识别 lastPID = ChildCvFindHand.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 4: if lastMode != Mode: lastMode = Mode if lastPID is not None and get_data.read_data( )[0] != 'K': os.kill(lastPID, signal.SIGKILL) ChildCvLinePatrol = subprocess.Popen([ "python3", "/home/pi/human_code/cv_line_patrol.py" ]) # 寻线 lastPID = ChildCvLinePatrol.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 5: if lastMode != Mode: lastMode = Mode if lastPID is not None and get_data.read_data( )[0] != 'K': os.kill(lastPID, signal.SIGKILL) ChildCvTrackStream = subprocess.Popen([ "python3", "/home/pi/human_code/cv_track_stream.py" ]) # 云台跟踪 lastPID = ChildCvTrackStream.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) else: lastMode = 0 if lastPID is not None and get_data.read_data( )[0] != 'K': os.kill(lastPID, signal.SIGKILL) lastPID = None self.request.sendall("Failed".encode()) pass except Exception as e: print(e) Flag = False
import callback_function as cf import get_data as gd import full_gradient as fg import numpy as np X_train, y_train = gd.read_data("train_sgd.txt") X_test, y_test = gd.read_data("test_sgd.txt") params = np.zeros(129 * 26 + 26**2) call_func = cf.callback_function(X_train, y_train, X_test, y_test, "bfgs_1e-2.txt", 0.01) call_func.delete_file() opt_params = fg.optimize(params, call_func) fg.print_accuracies(opt_params, X_train, y_train, X_test, y_test) call_func = cf.callback_function(X_train, y_train, X_test, y_test, "bfgs_1e-4.txt", 0.0001) call_func.delete_file() opt_params = fg.optimize(params, call_func) fg.print_accuracies(opt_params, X_train, y_train, X_test, y_test) call_func = cf.callback_function(X_train, y_train, X_test, y_test, "bfgs_1e-6.txt", 0.000001) call_func.delete_file() opt_params = fg.optimize(params, call_func) fg.print_accuracies(opt_params, X_train, y_train, X_test, y_test)
file.close() for i, line in enumerate(lines): split = line.split() print(str(i) + ": ", end = '') params = np.array(split[1:]).astype(np.float) w = gc.w_matrix(params) t = gc.t_matrix(params) predictions = fg.predict(X_test, w, t) accuracy = fg.accuracy(predictions, y_test) print(accuracy[0]) #only word accuracy so just accuracy[0] f_vals.append(str(1 - accuracy[0]) + "\n") file = open(outfile, 'w') file.writelines(f_vals) file.close() X_test, y_test = gd.read_data("test_sgd.txt") start = time.time() print_word_error("bfgs_1e-4.txt", "bfgs_1e-4_word_error.txt", X_test, y_test, 0.0001) print("time for first iteration %3f" %(time.time() - start)) print_word_error("sgd_1e-4.txt", "sgd_1e-4_word_error.txt", X_test, y_test, 0.0001) print_word_error("adam_1e-4.txt", "adam_1e-4_word_error.txt", X_test, y_test, 0.0001)
def handle(self): global action_num, action_times, inf_flag conn = self.request recv_data = b'' Flag = True while Flag: try: buf = conn.recv(1024) if buf == b'': Flag = False else: recv_data = recv_data + buf # Send the command to the serial port until receiving the complete command to prevent error while True: try: index = recv_data.index( b'\x55\x55') # Search 0x55 0x55 in the data if len( recv_data ) >= index + 3: # Whether the length of the data in the cache is sufficient recv_data = recv_data[index:] if recv_data[2] + 2 <= len( recv_data ): # Whether the length of the data in the cache is enough cmd = recv_data[0:(recv_data[2] + 2)] # Take the command recv_data = recv_data[( recv_data[2] + 3):] # Remove the taken command if cmd[0] and cmd[1] is 0x55: if cmd[2] == 0x08 and cmd[ 3] == 0x03: # Data length and control single servo command print('id', cmd[7]) id = cmd[7] pos = 0xffff & cmd[ 8] | 0xff00 & cmd[9] << 8 print('pos', pos) if id == 19: PWMServo.setServo(1, pos, 20) elif id == 20: PWMServo.setServo(2, pos, 20) else: pass elif cmd[2] == 0x05 and cmd[3] == 0x06: action_num = cmd[4] action_times = 0xffff & cmd[ 5] | 0xff00 & cmd[6] << 8 print('action', action_num) print('times', action_times) if action_times == 0: # Infinite imes print('action_times:', action_times) SSR.change_action_value( str(action_num), action_times) inf_flag = True else: if inf_flag: SSR.stop_action_group() inf_flag = False else: SSR.change_action_value( str(action_num), action_times) elif cmd[2] == 0x0b and cmd[3] == 0x03: PWMServo.setServo(1, 1500, 100) PWMServo.setServo(2, 1500, 100) time.sleep(0.1) data = get_data.read_data() print(int(data[0])) os.kill(int(data[0]), signal.SIGKILL) get_data.write_data("K", "0") if DEBUG is True: for i in cmd: print(hex(i)) print('*' * 30) else: break else: break except Exception as e: # The '\x55\x55' substring cannot be found in recv_data break recv_data = b'' action_times = None action_num = None except Exception as e: print(e) Flag = False break
def get_img_list(kit_path, class_mapping): all_imgs, classes_count, class_mapping = read_data(kit_path, class_mapping, dataset=['VOC2007']) print(classes_count) return all_imgs, classes_count, class_mapping
def handle(self): global action_num, action_times, inf_flag conn = self.request recv_data = b'' Flag = True stop = False t2 = 0 while Flag: try: buf = conn.recv(128) if buf == b'': Flag = False else: recv_data = recv_data + buf if len(recv_data) <= 13: # 解决断包问题,接收到完整命令后才发送到串口,防止出错 while True: try: index = recv_data.index( b'\x55\x55') # 搜索数据中的0x55 0x55 if len(recv_data) >= index + 3: # 缓存中的数据长度是否足够 recv_data = recv_data[index:] if recv_data[2] + 2 <= len( recv_data): # 缓存中的数据长度是否足够 cmd = recv_data[0:(recv_data[2] + 2)] # 取出命令 #print(cmd) recv_data = recv_data[( recv_data[2] + 3):] # 去除已经取出的命令 if cmd[0] and cmd[1] is 0x55: if cmd[2] == 0x08 and cmd[ 3] == 0x03: # 数据长度和控制单舵机命令 id = cmd[7] pos = 0xffff & cmd[ 8] | 0xff00 & (cmd[9] << 8) print('id:', cmd[7], 'pos:', pos) if id == 7: if 1900 < pos: pos = 1900 elif pos < 1200: pos = 1200 PWMServo.setServo( 1, 3000 - pos, 20) elif id == 6: PWMServo.setServo( 2, 3000 - pos, 20) else: pass elif cmd[2] == 0x05 and cmd[ 3] == 0x06: action_num = cmd[4] action_times = 0xffff & cmd[ 5] | 0xff00 & cmd[6] << 8 if action_num == 0: SSR.stop_action_group() SSR.change_action_value( 'stand_slow', 1) try: data = get_data.read_data( ) if data[0] != 'K': os.system( 'sudo kill -9 ' + str(data[0])) PWMServo.setServo( 1, 1500, 300) PWMServo.setServo( 2, 1500, 300) get_data.write_data( "K", "0") except BaseException as e: print(e) elif action_times == 0: # 无限次 print( 'action', action_num, 'times', action_times) SSR.change_action_value( str(action_num), action_times) stop = True else: print( 'action', action_num, 'times', action_times) if stop: SSR.stop_action_group() SSR.change_action_value( 'stand_slow', 1) stop = False else: print( 'action', action_num, 'times', action_times) SSR.change_action_value( str(action_num), action_times) elif cmd[2] == 0x0b and cmd[ 3] == 0x03: PWMServo.setServo(1, 1500, 100) PWMServo.setServo(2, 1500, 100) time.sleep(0.1) elif cmd[2] == 0x02 and cmd[ 3] == 0x0F: if SSR.action_finish(): try: time.sleep(0.05) v = math.ceil( serial_servo_read_vin( 1)) buf = [ 0x55, 0x55, 0x04, 0x0F, 0x00, 0x00 ] buf[4] = v & 0xFF buf[5] = ( v >> 8) & 0xFF conn.sendall( bytearray(buf)) except BaseException as e: print(e) if DEBUG is True: for i in cmd: print(hex(i)) print('*' * 30) else: break else: break except Exception as e: # 在recv_data 中搜不到 '\x55\x55'子串 break recv_data = b'' action_times = None action_num = None else: recv_data = b'' pass except Exception as e: print(e) Flag = False break
def handle(self): global lastMode global lastPID Flag = True while Flag: try: recv = self.request.recv(128) # Receive datav if recv == b'': Flag = False # Exit if it is empty else: self.buf += recv.decode() # Decode #print(self.buf) self.buf = re.sub( r'333333', '', self.buf, 10 ) # Appoint the client sends '3' to perform the heartbeat and remove the 3 from the character string s = re.search( r'mode=\d{1,2}', self.buf, re.I ) # Find the MODE= number character string in the formatted string if s: self.buf = "" # Clear all caches as long as find one Mode = int( s.group()[5:] ) # Get the value of mode from the character string print(Mode) data = get_data.read_data() if data[1] == "0": lastMode = 0 # According to the value of Mode, continue sending running signal to the corresponding subprocess, and the corresponding subprocess will run if Mode == 0: if lastMode != Mode: # Only if the last time mode is different from the current mode, the mode should convert lastMode = Mode if lastPID is not None: os.kill(lastPID, signal.SIGKILL) self.request.sendall( "OK".encode()) # Send "OK" to the client elif Mode == 1: if lastMode != Mode: lastMode = Mode if lastPID is not None: os.kill(lastPID, signal.SIGKILL) ChildCvColor = subprocess.Popen([ "python2", "/home/pi/hexapod/color.py" ]) # Colour identification lastPID = ChildCvColor.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 2: if lastMode != Mode: lastMode = Mode if lastPID is not None: os.kill(lastPID, signal.SIGKILL) ChildCvColorFollow = subprocess.Popen([ "python2", "/home/pi/hexapod/color_follow.py" ]) # Colour tracking lastPID = ChildCvColorFollow.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 3: if lastMode != Mode: lastMode = Mode if lastPID is not None: os.kill(lastPID, signal.SIGKILL) ChildCvColorFollowMove = subprocess.Popen([ "python2", "/home/pi/hexapod/color_follow_move.py" ]) # Ogranism colour tracking lastPID = ChildCvColorFollowMove.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 4: if lastMode != Mode: lastMode = Mode if lastPID is not None: os.kill(lastPID, signal.SIGKILL) ChildBalance = subprocess.Popen([ "python2", "/home/pi/hexapod/balance.py" ]) # Self balance lastPID = ChildBalance.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 5: if lastMode != Mode: lastMode = Mode if lastPID is not None: os.kill(lastPID, signal.SIGKILL) ChildCvFind = subprocess.Popen([ "python2", "/home/pi/hexapod/sonar.py" ]) # Ultrasonic aviodance lastPID = ChildCvFind.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) elif Mode == 6: if lastMode != Mode: lastMode = Mode if lastPID is not None: os.kill(lastPID, signal.SIGKILL) ChildCvLineFollow = subprocess.Popen([ "python2", "/home/pi/hexapod/linefollow.py" ]) # Line following lastPID = ChildCvLineFollow.pid get_data.write_data(str(lastPID), str(lastMode)) print("lastPID:", lastPID) self.request.sendall("OK".encode()) else: lastMode = 0 if lastPID is not None: os.kill(lastPID, signal.SIGKILL) lastPID = None self.request.sendall("Failed".encode()) pass except Exception as e: print(e) Flag = False
lines.append(line) file.close() for line in lines: split = line.split() print(split[0] + ": ", end='') params = np.array(split[1:]).astype(np.float) val = fg.func_to_minimize(params, X_train, y_train, l) print(val) f_vals.append(str(val) + "\n") file = open(outfile, 'w') file.writelines(f_vals) file.close() X_train, y_train = gd.read_data("train_sgd.txt") #time just this one to get a sense of how long the whole set will take start = time.time() print_function_values("bfgs_1e-2.txt", "bfgs_1e-2_f_vals.txt", X_train, y_train, 0.01) print("time for first iteration %3f" % (time.time() - start)) print_function_values("bfgs_1e-4.txt", "bfgs_1e-4_f_vals.txt", X_train, y_train, 0.0001) print_function_values("bfgs_1e-6.txt", "bfgs_1e-6_f_vals.txt", X_train, y_train, 0.000001) print_function_values("sgd_1e-2.txt", "sgd_1e-2_f_vals.txt", X_train, y_train,
def init_params(options): # check if weight path was passed via command line if options.input_weight_path: C.base_net_weights = options.input_weight_path else: # set the path to weights based on backend and model C.base_net_weights = nn.get_weight_path() train_classes = ['dog', 'cat', 'car'] all_imgs, classes_count, class_mapping = read_data(options.train_path, train_classes) if 'bg' not in classes_count: classes_count['bg'] = 0 class_mapping['bg'] = len(class_mapping) C.class_mapping = class_mapping print('Training images per class:') pprint.pprint(classes_count) print('Num classes (including bg) = {}'.format(len(classes_count))) config_output_filename = options.config_filename with open(config_output_filename, 'wb') as config_f: pickle.dump(C, config_f) print( 'Config has been written to {}, and can be loaded when testing to ensure correct results' .format(config_output_filename)) random.shuffle(all_imgs) # construct train and test sets train_imgs = [s for s in all_imgs if s['Imageset'] == 'train'] test_imgs = [s for s in all_imgs if s['Imageset'] == 'test'] print('Num train samples {}'.format(len(train_imgs))) print('Num test samples {}'.format(len(test_imgs))) data_gen_train = data_generators.get_anchor_gt(train_imgs, classes_count, C, nn.get_img_output_length, mode='train') data_gen_test = data_generators.get_anchor_gt(test_imgs, classes_count, C, nn.get_img_output_length, mode='test') input_shape_img = (None, None, 3) img_input = Input(shape=input_shape_img) roi_input = Input(shape=(None, 4)) # define the base network (resnet here, can be VGG, Inception, etc) shared_layers = nn.nn_base(img_input, trainable=True) # define the RPN, built on the base layers num_anchors = len(C.anchor_box_scales) * len(C.anchor_box_ratios) rpn = nn.rpn(shared_layers, num_anchors) classifier = nn.classifier(shared_layers, roi_input, C.num_rois, nb_classes=len(classes_count), trainable=True) model_rpn = Model(img_input, rpn[:2]) model_classifier = Model([img_input, roi_input], classifier) # this is a model that holds both the RPN and the classifier, used to load/save weights for the models model_all = Model([img_input, roi_input], rpn[:2] + classifier) try: print('loading weights from {}'.format(C.base_net_weights)) model_rpn.load_weights(C.base_net_weights, by_name=True) model_classifier.load_weights(C.base_net_weights, by_name=True) except IOError as e: print("I/O error({0}): {1}".format(e.errno, e.strerror)) except ValueError: print("Could not load model weights due to ValueError.") except: print('Could not load pre-trained model weights.') optimizer = Adam(lr=1e-5) optimizer_classifier = Adam(lr=1e-5) if options.loss_func == 'frcnn': model_rpn.compile(optimizer=optimizer, loss=[ losses.rpn_loss_cls(num_anchors), losses.rpn_loss_regr(num_anchors) ]) elif options.loss_func == 'focal_loss': print('Using focal loss') model_rpn.compile(optimizer=optimizer, loss=[ focal_loss.rpn_focal_loss_cls(num_anchors), focal_loss.rpn_smooth_l1_regr(num_anchors) ]) else: print('Loss function is invalid.') raise ValueError model_classifier.compile( optimizer=optimizer_classifier, loss=[ losses.class_loss_cls, losses.class_loss_regr(len(classes_count) - 1) ], metrics={'dense_class_{}'.format(len(classes_count)): 'accuracy'}) model_all.compile(optimizer='sgd', loss='mae') return data_gen_train, data_gen_test, model_rpn, model_classifier, model_all
def test_article_similarity(art1, art2): return article_similarity(art1, art2) if __name__ == '__main__': # articles = read_data() # sentences = test_split_sentence(articles[0]) # sentences = [test_split_word(sentence) for sentence in sentences] #print(test_text_rank(sentences)) #print(test_extract_abstract_by_text_rank(sentences)) # with open('input.txt', 'w', encoding='utf-8') as f: # f.write(articles[0]) # with open('output.txt', 'w', encoding='utf-8') as f: # f.write(test_extract_abstract_by_text_rank(sentences)) #print(get_keywords(sentences)) # sentences1 = test_split_sentence(articles[1]) # sentences1 = [test_split_word(sentence) for sentence in sentences1] #print(test_article_similarity(sentences, sentences1)) articles = read_data() with open('test.txt', 'w', encoding='utf-8') as f: extractor = SimpleExtractor(articles[0]) f.write(json.dumps(extractor.get_suffix(), ensure_ascii=False)) f.write('\n') f.write(json.dumps(extractor.get_reverse_suffix(), ensure_ascii=False)) f.write('\n') f.write(json.dumps(extractor.get_term_freq(), ensure_ascii=False)) f.write('\n') f.write(json.dumps(extractor.extract(), ensure_ascii=False))