def upload() -> dict: cli_uuid = unquote(get_arg("uuid")) print("post from:", cli_uuid) if len(cli_uuid) == 2: if os.path.exists(user_dir + cli_uuid): length = int(request.headers.get('content-length')) print("准备接收:", length, "bytes") if length < MAXBUFFSZ: return save_img(request.get_data(), cli_uuid, image_dir, info_json_path) else: return save_img(request.stream.read(length), cli_uuid, image_dir, info_json_path) else: return {"stat":"noid"} else: return {"stat":"invid"}
def run(self) -> None: while True: r = self.p.request('GET', RANDOM_IMG_API, preload_content=False) data = r.read() print(save_img(data, "涩酱", image_dir, json_dir)) r.release_conn() sleep(DELAY * THREAD_NUM)
def run_infer(weights_file, labels_file, image_path, out_filename): model = YOLOv4Model() model.load_weights(weights_file) img, input = read_img(image_path, 608) cls_names = open(labels_file, "r").read().split("\n") boxes, scores, labels = inference.infer(model, cls_names, input) pixels = add_bboxes(img, boxes, scores, labels) if out_filename: save_img(out_filename, pixels) else: draw_img(pixels)
async def upload() -> dict: cli_uuid = unquote(get_arg("uuid")) print("post from:", cli_uuid) if len(cli_uuid) == 2: if os.path.exists(user_dir + cli_uuid): return save_img(await request.get_data(), cli_uuid, image_dir, info_json_path) else: return {"stat":"noid"} else: return {"stat":"invid"}
def run_infer(weights_file, labels_file, image_path, out_filename): cls_names = open(labels_file, "r").read().split("\n") model = YOLOv4Model() model.load_weights(weights_file) img, input = read_img(image_path, 608) prediction = model.predict(input) boxes, scores, labels = inference.decode_prediction(prediction, len(cls_names)) labels = [cls_names[cls] for cls in labels] pixels = add_bboxes(img, boxes, scores, labels) if out_filename: save_img(out_filename, pixels) else: draw_img(pixels)
def upform() -> dict: cli_uuid = unquote(get_arg("uuid")) print("post from:", cli_uuid) if len(cli_uuid) == 2: if os.path.exists(user_dir + cli_uuid): re = [] for f in request.files.getlist("img"): re.append({"name":f.filename, **save_img(f.read(), cli_uuid, image_dir, info_json_path)}) return {"result": re} else: return {"stat":"noid"} else: return {"stat":"invid"}
def load_and_train(options): # unpack parameters sty_imgs = options.sty_imgs sty_weights = np.array(options.sty_weights) cont_img = options.cont_img output_file = options.output_file output_scale = options.output_scale learn_rate = options.learn_rate alpha = np.float32(options.alpha) beta = np.float32(options.beta) num_epoch = options.num_epoch vgg19_loc = options.vgg19_loc # load images and vgg19 coefficients sty_features = load_sty_features() cont_feature = load_cont_feature() cont = load_cont_img(cont_img, output_scale) vgg_obj = vgg.VGG19(vgg19_loc) cont_ten = comp_cont_ten(cont, cont_feature, vgg_obj) gram, gram_coef = comp_sty_gram(load_sty_imgs(sty_imgs), sty_weights, sty_features, vgg_obj) # model cont_remix = tf.Variable(cont) vgg_obj.build_upto(cont_remix, 'pool5', False) # style loss function gamma = np.float32(1.0 / len(sty_features)) gram_style = {} for style in sty_features: this_shape = vgg_obj.layers[style].get_shape().as_list() this_Ml = this_shape[1] * this_shape[2] reshaped = tf.reshape(vgg_obj.layers[style], (-1, this_shape[3])) gram_style[style] = tf.matmul(tf.transpose(reshaped), reshaped) / (this_Ml**2) loss_style = tf.constant(np.float32(0.0)) for style in sty_features: loss_style += tf.reduce_sum( tf.square(gram_style[style] - gram[style])) * gram_coef[style] # content loss function loss_content = tf.reduce_mean( tf.square(vgg_obj.layers[cont_feature] - cont_ten)) # punish local pixel noise loss_noise = tf.reduce_mean( tf.abs( tf.nn.max_pool(cont_remix, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='VALID') - tf.nn.max_pool(-cont_remix, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='VALID'))) # train step loss = gamma * loss_style + alpha * loss_content + beta * loss_noise err = float('inf') train_step = tf.train.AdamOptimizer(learn_rate).minimize(loss) with tf.Session() as sess: tf.initialize_all_variables().run() for idx in range(num_epoch): sess.run(train_step) # list all errors this_loss_content = alpha * loss_content.eval() this_loss_style = gamma * loss_style.eval() this_loss_noise = beta * loss_noise.eval() this_err = this_loss_content + this_loss_style + this_loss_noise print('epoch', idx, ': content loss', this_loss_content, 'style loss', this_loss_style, 'noise loss', this_loss_noise) if this_err < err: err = this_err output = cont_remix.eval()[0, :, :, :] # save image img.save_img(output_file, img.revert_img(output))