void label_classifier(char *datacfg, char *filename, char *weightfile) { int i; network *net = load_network(filename, weightfile, 0); set_batch_network(net, 1); srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "names", "data/labels.list"); char *test_list = option_find_str(options, "test", "data/train.list"); int classes = option_find_int(options, "classes", 2); char **labels = get_labels(label_list); list *plist = get_paths(test_list); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); for(i = 0; i < m; ++i){ image im = load_image_color(paths[i], 0, 0); image resized = resize_min(im, net->w); image crop = crop_image(resized, (resized.w - net->w)/2, (resized.h - net->h)/2, net->w, net->h); float *pred = network_predict(net, crop.data); if(resized.data != im.data) free_image(resized); free_image(im); free_image(crop); int ind = max_index(pred, classes); printf("%s\n", labels[ind]); } }
void PixelsMovement::getDraggedImageCopy(base::UniquePtr<Image>& outputImage, base::UniquePtr<Mask>& outputMask) { gfx::Rect bounds = m_currentData.transformedBounds(); base::UniquePtr<Image> image(Image::create(m_sprite->pixelFormat(), bounds.w, bounds.h)); drawImage(image, bounds.origin(), false); // Draw mask without shrinking it, so the mask size is equal to the // "image" render. base::UniquePtr<Mask> mask(new Mask); drawMask(mask, false); // Now we can shrink and crop the image. gfx::Rect oldMaskBounds = mask->bounds(); mask->shrink(); gfx::Rect newMaskBounds = mask->bounds(); if (newMaskBounds != oldMaskBounds) { newMaskBounds.x -= oldMaskBounds.x; newMaskBounds.y -= oldMaskBounds.y; image.reset(crop_image(image, newMaskBounds.x, newMaskBounds.y, newMaskBounds.w, newMaskBounds.h, 0)); } outputImage.reset(image.release()); outputMask.reset(mask.release()); }
ImageRef ExpandCelCanvas::trimDstImage(const gfx::Rect& bounds) const { return ImageRef( crop_image(m_dstImage.get(), bounds.x, bounds.y, bounds.w, bounds.h, m_dstImage->maskColor())); }
void validate_classifier_single(char *datacfg, char *filename, char *weightfile) { int i, j; network *net = load_network(filename, weightfile, 0); set_batch_network(net, 1); srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "labels", "data/labels.list"); char *leaf_list = option_find_str(options, "leaves", 0); if(leaf_list) change_leaves(net->hierarchy, leaf_list); char *valid_list = option_find_str(options, "valid", "data/train.list"); int classes = option_find_int(options, "classes", 2); int topk = option_find_int(options, "top", 1); char **labels = get_labels(label_list); list *plist = get_paths(valid_list); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); float avg_acc = 0; float avg_topk = 0; int *indexes = calloc(topk, sizeof(int)); for(i = 0; i < m; ++i){ int class = -1; char *path = paths[i]; for(j = 0; j < classes; ++j){ if(strstr(path, labels[j])){ class = j; break; } } image im = load_image_color(paths[i], 0, 0); image resized = resize_min(im, net->w); image crop = crop_image(resized, (resized.w - net->w)/2, (resized.h - net->h)/2, net->w, net->h); //show_image(im, "orig"); //show_image(crop, "cropped"); //cvWaitKey(0); float *pred = network_predict(net, crop.data); if(net->hierarchy) hierarchy_predictions(pred, net->outputs, net->hierarchy, 1, 1); if(resized.data != im.data) free_image(resized); free_image(im); free_image(crop); top_k(pred, classes, topk, indexes); if(indexes[0] == class) avg_acc += 1; for(j = 0; j < topk; ++j){ if(indexes[j] == class) avg_topk += 1; } printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1)); } }
void test_lsd(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; int i, imlayer = 0; for (i = 0; i < net.n; ++i) { if (net.layers[i].out_c == 3) { imlayer = i; printf("%d\n", i); break; } } while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); image resized = resize_min(im, net.w); image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h); //grayscale_image_3c(crop); float *X = crop.data; time=clock(); network_predict(net, X); image out = get_network_image_layer(net, imlayer); //yuv_to_rgb(out); constrain_image(out); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); show_image(out, "out"); show_image(crop, "crop"); save_image(out, "out"); #ifdef OPENCV cvWaitKey(0); #endif free_image(im); free_image(resized); free_image(crop); if (filename) break; } }
static void save_image(img_t *img, char *output_dir) { char filename[1024]; // create the needed output directory if not present snprintf(filename, 1024, "%s/13/", output_dir); mkdir(filename, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); snprintf(filename, 1024, "%s/13/%d/", output_dir, img->tileX); mkdir(filename, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); snprintf(filename, 1024, "%s/13/%d/%d.png", output_dir, img->tileX, img->tileY); FILE *file = fopen(filename, "wb"); crop_image(img, 6, file); fclose(file); }
int main(int argc, char **argv) { char* input = argv[1]; int channels = 3; list *plist = get_paths(input); //int N = plist->size; char **paths = (char **)list_to_array(plist); int m = plist->size; int count_label = 0; char labelpath[256]; sprintf(labelpath, "../labels/01_5_1.txt"); int count = 0; box_label *boxes = read_boxes(labelpath, &count); for (int i = 0; i < m; i++) { if (!paths[i]) { printf("load error!\n"); break; } image im = load_image(paths[i], 0, 0, channels); printf("load %s", paths[i]); float x, y, w, h; count_label++; x = boxes[i].x; y = boxes[i].y; w = boxes[i].w; h = boxes[i].h; box box = { x, y, w, h }; char *save_path1 = get_basename(paths[i]); char save_path[256]; sprintf(save_path, "./images/%s", save_path1); //show_image(im, save_path); image crop_im = crop_image(im, x,y,w,h); printf(" %f %f %f %f \n", x,y,w,h); show_image(crop_im, save_path); free_image(crop_im); free_image(im); } }
// static Image* Image::createCopy(const Image* image, const ImageBufferPtr& buffer) { ASSERT(image); return crop_image(image, 0, 0, image->getWidth(), image->getHeight(), image->getMaskColor(), buffer); }
void copy_cel(ContextWriter& writer) { Document* document = writer.document(); Sprite* sprite = writer.sprite(); UndoTransaction undo(writer.context(), "Move Cel", undo::ModifyDocument); Cel *src_cel, *dst_cel; ASSERT(src_layer != NULL); ASSERT(dst_layer != NULL); ASSERT(src_frame >= 0 && src_frame < sprite->getTotalFrames()); ASSERT(dst_frame >= 0 && dst_frame < sprite->getTotalFrames()); src_cel = static_cast<LayerImage*>(src_layer)->getCel(src_frame); dst_cel = static_cast<LayerImage*>(dst_layer)->getCel(dst_frame); // Remove the 'dst_cel' (if it exists) because it must be replaced // with 'src_cel' if ((dst_cel != NULL) && (!dst_layer->isBackground() || src_cel != NULL)) remove_cel(sprite, undo, static_cast<LayerImage*>(dst_layer), dst_cel); // Move the cel in the same layer. if (src_cel != NULL) { Image *src_image = sprite->getStock()->getImage(src_cel->getImage()); Image *dst_image; int image_index; int dst_cel_x; int dst_cel_y; int dst_cel_opacity; // If we are moving a cel from a transparent layer to the // background layer, we have to clear the background of the image. if (!src_layer->isBackground() && dst_layer->isBackground()) { dst_image = crop_image(src_image, -src_cel->getX(), -src_cel->getY(), sprite->getWidth(), sprite->getHeight(), 0); clear_image(dst_image, app_get_color_to_clear_layer(dst_layer)); composite_image(dst_image, src_image, src_cel->getX(), src_cel->getY(), 255, BLEND_MODE_NORMAL); dst_cel_x = 0; dst_cel_y = 0; dst_cel_opacity = 255; } else { dst_image = Image::createCopy(src_image); dst_cel_x = src_cel->getX(); dst_cel_y = src_cel->getY(); dst_cel_opacity = src_cel->getOpacity(); } // Add the image in the stock image_index = sprite->getStock()->addImage(dst_image); if (undo.isEnabled()) undo.pushUndoer(new undoers::AddImage(undo.getObjects(), sprite->getStock(), image_index)); // Create the new cel dst_cel = new Cel(dst_frame, image_index); dst_cel->setPosition(dst_cel_x, dst_cel_y); dst_cel->setOpacity(dst_cel_opacity); if (undo.isEnabled()) undo.pushUndoer(new undoers::AddCel(undo.getObjects(), dst_layer, dst_cel)); static_cast<LayerImage*>(dst_layer)->addCel(dst_cel); } undo.commit(); document->notifyCelCopied(src_layer, src_frame, dst_layer, dst_frame); set_frame_to_handle(NULL, FrameNumber(0), NULL, FrameNumber(0)); }
int main(int argc, char **argv) { int ret, tmp; img_t img; char *output_dir; FILE *out_file; if (argc < 5) { printf("Usage:\npacked_tilegen <input> <output_dir> <tileX> <tileY> [cardinal direction N|E|S|W\n"); exit(1); } printf("Read file \"%s\"\n", argv[1]); if (strstr(argv[1], ".png")) ret = read_png(argv[1], &img); else if (strstr(argv[1], ".ppm")) ret = read_ppm(argv[1], &img); else { printf("Unknown format!\n"); exit(2); } output_dir = argv[2]; if (ret) { printf("Could not read png input file! %d\n", ret); exit(ret); } img.tileX = atoi(argv[3]); img.tileY = atoi(argv[4]); if (argc > 5) { switch(argv[5][0]) { case 'n': case 'N': // keep defaults break; case 'e': case 'E': tmp = img.tileX; img.tileX = img.tileY; img.tileY = (2<<12) - 1 - tmp; break; case 's': case 'S': img.tileX = (2<<12) - 1 - img.tileX; img.tileY = (2<<12) - 1 - img.tileY; break; case 'w': case 'W': tmp = img.tileX; img.tileX = (2<<12) - 1 - img.tileY; img.tileY = tmp; break; default: fprintf(stderr, "Unknown cardinal direction \"%s\". Default to north\n", argv[5]); } } // create pack file for writing out_file = open_packed_file(output_dir, img.tileX, img.tileY); tmp = 0; while (1) { // crop image at given zoom level.. crop_image(&img, tmp++, out_file); scale_image(&img); if (img.sizeX == TILE_SIZE) { save_image(&img, output_dir); break; } } close_packed_file(out_file); return 0; }
ExpandCelCanvas::ExpandCelCanvas(Context* context, TiledMode tiledMode, UndoTransaction& undo) : m_cel(NULL) , m_celImage(NULL) , m_celCreated(false) , m_closed(false) , m_committed(false) , m_undo(undo) { create_buffers(); DocumentLocation location = context->getActiveLocation(); m_document = location.document(); m_sprite = location.sprite(); m_layer = location.layer(); if (m_layer->isImage()) { m_cel = static_cast<LayerImage*>(m_layer)->getCel(location.frame()); if (m_cel) m_celImage = m_sprite->getStock()->getImage(m_cel->getImage()); } // If there is no Cel if (m_cel == NULL) { // Create the image m_celImage = Image::create(m_sprite->getPixelFormat(), m_sprite->getWidth(), m_sprite->getHeight()); clear_image(m_celImage, m_sprite->getTransparentColor()); // Create the cel m_cel = new Cel(location.frame(), 0); static_cast<LayerImage*>(m_layer)->addCel(m_cel); m_celCreated = true; } m_originalCelX = m_cel->getX(); m_originalCelY = m_cel->getY(); // Region to draw int x1, y1, x2, y2; if (tiledMode == TILED_NONE) { // Non-tiled x1 = MIN(m_cel->getX(), 0); y1 = MIN(m_cel->getY(), 0); x2 = MAX(m_cel->getX()+m_celImage->getWidth(), m_sprite->getWidth()); y2 = MAX(m_cel->getY()+m_celImage->getHeight(), m_sprite->getHeight()); } else { // Tiled x1 = 0; y1 = 0; x2 = m_sprite->getWidth(); y2 = m_sprite->getHeight(); } // create two copies of the image region which we'll modify with the tool m_srcImage = crop_image(m_celImage, x1-m_cel->getX(), y1-m_cel->getY(), x2-x1, y2-y1, m_sprite->getTransparentColor(), src_buffer); m_dstImage = Image::createCopy(m_srcImage, dst_buffer); // We have to adjust the cel position to match the m_dstImage // position (the new m_dstImage will be used in RenderEngine to // draw this cel). m_cel->setPosition(x1, y1); }
void BackgroundBuilder::run(){ const QRect win_dim = QApplication::desktop()->screenGeometry(); std::cout << "Desktop is: " << win_dim.width() << "x" << win_dim.height() << "\n"; const float desktop_aspect = static_cast<float>(win_dim.width()) / win_dim.height(); const float image_aspect = static_cast<float>(original->width()) / original->height(); std::cout << "Desktop aspect = " << desktop_aspect << ", image = " << image_aspect << "\n"; auto pipeline_start = std::chrono::high_resolution_clock::now(); // If the image is has a higher aspect ratio than the desktop we'll want to scale y // to fit and let x be croppped, otherwise we do the opposite int scaled_w = 0; int scaled_h = 0; if (desktop_aspect < image_aspect){ scaled_h = win_dim.height() + 32; scaled_w = scaled_h * image_aspect; } else { scaled_w = win_dim.width() + 32; scaled_h = scaled_w * (1.0 / image_aspect); } //std::cout << "scaling image to " << scaled_w << "x" << scaled_h << "\n"; QImage scaled_background{ scaled_w, scaled_h, QImage::Format_RGB32 }; auto start = std::chrono::high_resolution_clock::now(); resize_image(*blurred, scaled_background); auto end = std::chrono::high_resolution_clock::now(); //std::cout << "resize took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() // << "ms\n"; //scaled_background.save(QString("scaled_background.jpg"), "JPEG"); int crop_x = (scaled_w - win_dim.width()) / 2; int start_x = crop_x; int end_x = scaled_w - crop_x; // Sometimes our crop may be off by a few pixels, so just drop the extra ones if we don't match if (end_x - start_x != win_dim.width()){ //std::cout << "width mismatch of " << end_x - start_x - win_dim.width() << "pixels\n"; end_x -= end_x - start_x - win_dim.width(); } int crop_y = (scaled_h - win_dim.height()) / 2; int start_y = crop_y; int end_y = scaled_h - crop_y; if (end_y - start_y != win_dim.height()){ //std::cout << "height mismatch of " << end_y - start_y - win_dim.height() << "pixels\n"; end_y -= end_y - start_y - win_dim.height(); } QSharedPointer<QImage> background(new QImage{win_dim.width(), win_dim.height(), QImage::Format_RGB32}); start = std::chrono::high_resolution_clock::now(); crop_image(scaled_background, *background, start_x, end_x, start_y, end_y); end = std::chrono::high_resolution_clock::now(); //std::cout << "crop took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() // << "ms\n"; //background->save(QString("cropped_background.jpg"), "JPEG"); //std::cout << "compositing original image with blurred background\n"; int centered_w = 0; int centered_h = 0; float border_percent = 0.04f; if (desktop_aspect > image_aspect) { centered_h = win_dim.height() - border_percent * win_dim.height(); centered_w = centered_h * image_aspect; //std::cout << "border size of " << border_percent * win_dim.height() << " along x\n"; } else { centered_w = win_dim.width() - border_percent * win_dim.width(); centered_h = centered_w * (1.0 / image_aspect); //std::cout << "border size of " << border_percent * win_dim.width() << " along y\n"; } //std::cout << "Centered image will be scaled to " << centered_w << "x" << centered_h << "\n"; QImage centered_image{centered_w, centered_h, QImage::Format_RGB32}; start = std::chrono::high_resolution_clock::now(); resize_image(*original, centered_image); end = std::chrono::high_resolution_clock::now(); //std::cout << "scaling centered image took " // << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms\n"; //centered_image.save(QString("centered_image.jpg"), "JPEG"); start = std::chrono::high_resolution_clock::now(); composite_background(centered_image, *background, *background); end = std::chrono::high_resolution_clock::now(); //std::cout << "compositing took " // << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms\n"; auto pipeline_end = std::chrono::high_resolution_clock::now(); std::cout << "Entire pipeline to build background took " << std::chrono::duration_cast<std::chrono::milliseconds>(pipeline_end - pipeline_start).count() << "ms\n"; std::cout << "done making background, Saving background to background.png\n"; // Note: in the future we'll only save to the temp dir if we need to (on Windows we must pass a filename) background->save(QString::fromStdString("background.png"), "PNG"); background->save(QDir::tempPath() + QString::fromStdString("/background.png"), "PNG"); std::cout << "background saved\n"; emit finished(background); }
void run_nightmare(int argc, char **argv) { srand(0); if(argc < 4){ fprintf(stderr, "usage: %s %s [cfg] [weights] [image] [layer] [options! (optional)]\n", argv[0], argv[1]); return; } char *cfg = argv[2]; char *weights = argv[3]; char *input = argv[4]; int max_layer = atoi(argv[5]); int range = find_int_arg(argc, argv, "-range", 1); int rounds = find_int_arg(argc, argv, "-rounds", 1); int iters = find_int_arg(argc, argv, "-iters", 10); int octaves = find_int_arg(argc, argv, "-octaves", 4); float zoom = find_float_arg(argc, argv, "-zoom", 1.); float rate = find_float_arg(argc, argv, "-rate", .04); float thresh = find_float_arg(argc, argv, "-thresh", 1.); float rotate = find_float_arg(argc, argv, "-rotate", 0); char *prefix = find_char_arg(argc, argv, "-prefix", 0); network net = parse_network_cfg(cfg); load_weights(&net, weights); char *cfgbase = basecfg(cfg); char *imbase = basecfg(input); set_batch_network(&net, 1); image im = load_image_color(input, 0, 0); if(0){ float scale = 1; if(im.w > 512 || im.h > 512){ if(im.w > im.h) scale = 512.0/im.w; else scale = 512.0/im.h; } image resized = resize_image(im, scale*im.w, scale*im.h); free_image(im); im = resized; } int e; int n; for(e = 0; e < rounds; ++e){ fprintf(stderr, "Iteration: "); fflush(stderr); for(n = 0; n < iters; ++n){ fprintf(stderr, "%d, ", n); fflush(stderr); int layer = max_layer + rand()%range - range/2; int octave = rand()%octaves; optimize_picture(&net, im, layer, 1/pow(1.33333333, octave), rate, thresh); } fprintf(stderr, "done\n"); if(0){ image g = grayscale_image(im); free_image(im); im = g; } char buff[256]; if (prefix){ sprintf(buff, "%s/%s_%s_%d_%06d",prefix, imbase, cfgbase, max_layer, e); }else{ sprintf(buff, "%s_%s_%d_%06d",imbase, cfgbase, max_layer, e); } printf("%d %s\n", e, buff); save_image(im, buff); //show_image(im, buff); //cvWaitKey(0); if(rotate){ image rot = rotate_image(im, rotate); free_image(im); im = rot; } image crop = crop_image(im, im.w * (1. - zoom)/2., im.h * (1.-zoom)/2., im.w*zoom, im.h*zoom); image resized = resize_image(crop, im.w, im.h); free_image(im); free_image(crop); im = resized; } }
/* * BEE::Sprite::crop_image_height() - Crop the image to the given height * @new_crop_height: the new height to crop the image to */ int BEE::Sprite::crop_image_height(int new_crop_height) { crop.h = new_crop_height; // Set the height return crop_image(crop); // Return the status of the cropping }
void run_nightmare(int argc, char **argv) { srand(0); if(argc < 4){ fprintf(stderr, "usage: %s %s [cfg] [weights] [image] [layer] [options! (optional)]\n", argv[0], argv[1]); return; } char *cfg = argv[2]; char *weights = argv[3]; char *input = argv[4]; int max_layer = atoi(argv[5]); int range = find_int_arg(argc, argv, "-range", 1); int norm = find_int_arg(argc, argv, "-norm", 1); int rounds = find_int_arg(argc, argv, "-rounds", 1); int iters = find_int_arg(argc, argv, "-iters", 10); int octaves = find_int_arg(argc, argv, "-octaves", 4); float zoom = find_float_arg(argc, argv, "-zoom", 1.); float rate = find_float_arg(argc, argv, "-rate", .04); float thresh = find_float_arg(argc, argv, "-thresh", 1.); float rotate = find_float_arg(argc, argv, "-rotate", 0); float momentum = find_float_arg(argc, argv, "-momentum", .9); float lambda = find_float_arg(argc, argv, "-lambda", .01); char *prefix = find_char_arg(argc, argv, "-prefix", 0); int reconstruct = find_arg(argc, argv, "-reconstruct"); int smooth_size = find_int_arg(argc, argv, "-smooth", 1); network net = parse_network_cfg(cfg); load_weights(&net, weights); char *cfgbase = basecfg(cfg); char *imbase = basecfg(input); set_batch_network(&net, 1); image im = load_image_color(input, 0, 0); if(0){ float scale = 1; if(im.w > 512 || im.h > 512){ if(im.w > im.h) scale = 512.0/im.w; else scale = 512.0/im.h; } image resized = resize_image(im, scale*im.w, scale*im.h); free_image(im); im = resized; } float *features = 0; image update; if (reconstruct){ resize_network(&net, im.w, im.h); int zz = 0; network_predict(net, im.data); image out_im = get_network_image(net); image crop = crop_image(out_im, zz, zz, out_im.w-2*zz, out_im.h-2*zz); //flip_image(crop); image f_im = resize_image(crop, out_im.w, out_im.h); free_image(crop); printf("%d features\n", out_im.w*out_im.h*out_im.c); im = resize_image(im, im.w, im.h); f_im = resize_image(f_im, f_im.w, f_im.h); features = f_im.data; int i; for(i = 0; i < 14*14*512; ++i){ features[i] += rand_uniform(-.19, .19); } free_image(im); im = make_random_image(im.w, im.h, im.c); update = make_image(im.w, im.h, im.c); } int e; int n; for(e = 0; e < rounds; ++e){ fprintf(stderr, "Iteration: "); fflush(stderr); for(n = 0; n < iters; ++n){ fprintf(stderr, "%d, ", n); fflush(stderr); if(reconstruct){ reconstruct_picture(net, features, im, update, rate, momentum, lambda, smooth_size, 1); //if ((n+1)%30 == 0) rate *= .5; show_image(im, "reconstruction"); #ifdef OPENCV cvWaitKey(10); #endif }else{ int layer = max_layer + rand()%range - range/2; int octave = rand()%octaves; optimize_picture(&net, im, layer, 1/pow(1.33333333, octave), rate, thresh, norm); } } fprintf(stderr, "done\n"); if(0){ image g = grayscale_image(im); free_image(im); im = g; } char buff[256]; if (prefix){ sprintf(buff, "%s/%s_%s_%d_%06d",prefix, imbase, cfgbase, max_layer, e); }else{ sprintf(buff, "%s_%s_%d_%06d",imbase, cfgbase, max_layer, e); } printf("%d %s\n", e, buff); save_image(im, buff); //show_image(im, buff); //cvWaitKey(0); if(rotate){ image rot = rotate_image(im, rotate); free_image(im); im = rot; } image crop = crop_image(im, im.w * (1. - zoom)/2., im.h * (1.-zoom)/2., im.w*zoom, im.h*zoom); image resized = resize_image(crop, im.w, im.h); free_image(im); free_image(crop); im = resized; } }
void move_cel(ContextWriter& writer) { Document* document = writer.document(); Sprite* sprite = writer.sprite(); Cel *src_cel, *dst_cel; ASSERT(src_layer != NULL); ASSERT(dst_layer != NULL); ASSERT(src_frame >= 0 && src_frame < sprite->getTotalFrames()); ASSERT(dst_frame >= 0 && dst_frame < sprite->getTotalFrames()); if (src_layer->isBackground()) { copy_cel(writer); return; } src_cel = static_cast<LayerImage*>(src_layer)->getCel(src_frame); dst_cel = static_cast<LayerImage*>(dst_layer)->getCel(dst_frame); UndoTransaction undo(writer.context(), "Move Cel", undo::ModifyDocument); /* remove the 'dst_cel' (if it exists) because it must be replaced with 'src_cel' */ if ((dst_cel != NULL) && (!dst_layer->isBackground() || src_cel != NULL)) remove_cel(sprite, undo, static_cast<LayerImage*>(dst_layer), dst_cel); /* move the cel in the same layer */ if (src_cel != NULL) { if (src_layer == dst_layer) { if (undo.isEnabled()) undo.pushUndoer(new undoers::SetCelFrame(undo.getObjects(), src_cel)); src_cel->setFrame(dst_frame); } /* move the cel in different layers */ else { if (undo.isEnabled()) undo.pushUndoer(new undoers::RemoveCel(undo.getObjects(), src_layer, src_cel)); static_cast<LayerImage*>(src_layer)->removeCel(src_cel); src_cel->setFrame(dst_frame); /* if we are moving a cel from a transparent layer to the background layer, we have to clear the background of the image */ if (!src_layer->isBackground() && dst_layer->isBackground()) { Image* src_image = sprite->getStock()->getImage(src_cel->getImage()); Image* dst_image = crop_image(src_image, -src_cel->getX(), -src_cel->getY(), sprite->getWidth(), sprite->getHeight(), 0); if (undo.isEnabled()) { undo.pushUndoer(new undoers::ReplaceImage(undo.getObjects(), sprite->getStock(), src_cel->getImage())); undo.pushUndoer(new undoers::SetCelPosition(undo.getObjects(), src_cel)); undo.pushUndoer(new undoers::SetCelOpacity(undo.getObjects(), src_cel)); } clear_image(dst_image, app_get_color_to_clear_layer(dst_layer)); composite_image(dst_image, src_image, src_cel->getX(), src_cel->getY(), 255, BLEND_MODE_NORMAL); src_cel->setPosition(0, 0); src_cel->setOpacity(255); sprite->getStock()->replaceImage(src_cel->getImage(), dst_image); delete src_image; } if (undo.isEnabled()) undo.pushUndoer(new undoers::AddCel(undo.getObjects(), dst_layer, src_cel)); static_cast<LayerImage*>(dst_layer)->addCel(src_cel); } } undo.commit(); document->notifyCelMoved(src_layer, src_frame, dst_layer, dst_frame); set_frame_to_handle(NULL, FrameNumber(0), NULL, FrameNumber(0)); }
void validate_classifier_10(char *datacfg, char *filename, char *weightfile) { int i, j; network *net = load_network(filename, weightfile, 0); set_batch_network(net, 1); srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "labels", "data/labels.list"); char *valid_list = option_find_str(options, "valid", "data/train.list"); int classes = option_find_int(options, "classes", 2); int topk = option_find_int(options, "top", 1); char **labels = get_labels(label_list); list *plist = get_paths(valid_list); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); float avg_acc = 0; float avg_topk = 0; int *indexes = calloc(topk, sizeof(int)); for(i = 0; i < m; ++i){ int class = -1; char *path = paths[i]; for(j = 0; j < classes; ++j){ if(strstr(path, labels[j])){ class = j; break; } } int w = net->w; int h = net->h; int shift = 32; image im = load_image_color(paths[i], w+shift, h+shift); image images[10]; images[0] = crop_image(im, -shift, -shift, w, h); images[1] = crop_image(im, shift, -shift, w, h); images[2] = crop_image(im, 0, 0, w, h); images[3] = crop_image(im, -shift, shift, w, h); images[4] = crop_image(im, shift, shift, w, h); flip_image(im); images[5] = crop_image(im, -shift, -shift, w, h); images[6] = crop_image(im, shift, -shift, w, h); images[7] = crop_image(im, 0, 0, w, h); images[8] = crop_image(im, -shift, shift, w, h); images[9] = crop_image(im, shift, shift, w, h); float *pred = calloc(classes, sizeof(float)); for(j = 0; j < 10; ++j){ float *p = network_predict(net, images[j].data); if(net->hierarchy) hierarchy_predictions(p, net->outputs, net->hierarchy, 1, 1); axpy_cpu(classes, 1, p, 1, pred, 1); free_image(images[j]); } free_image(im); top_k(pred, classes, topk, indexes); free(pred); if(indexes[0] == class) avg_acc += 1; for(j = 0; j < topk; ++j){ if(indexes[j] == class) avg_topk += 1; } printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1)); } }
void try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int layer_num) { network *net = load_network(cfgfile, weightfile, 0); set_batch_network(net, 1); srand(2222222); list *options = read_data_cfg(datacfg); char *name_list = option_find_str(options, "names", 0); if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list"); int top = option_find_int(options, "top", 1); int i = 0; char **names = get_labels(name_list); clock_t time; int *indexes = calloc(top, sizeof(int)); char buff[256]; char *input = buff; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image orig = load_image_color(input, 0, 0); image r = resize_min(orig, 256); image im = crop_image(r, (r.w - 224 - 1)/2 + 1, (r.h - 224 - 1)/2 + 1, 224, 224); float mean[] = {0.48263312050943, 0.45230225481413, 0.40099074308742}; float std[] = {0.22590347483426, 0.22120921437787, 0.22103996251583}; float var[3]; var[0] = std[0]*std[0]; var[1] = std[1]*std[1]; var[2] = std[2]*std[2]; normalize_cpu(im.data, mean, var, 1, 3, im.w*im.h); float *X = im.data; time=clock(); float *predictions = network_predict(net, X); layer l = net->layers[layer_num]; for(i = 0; i < l.c; ++i){ if(l.rolling_mean) printf("%f %f %f\n", l.rolling_mean[i], l.rolling_variance[i], l.scales[i]); } #ifdef GPU cuda_pull_array(l.output_gpu, l.output, l.outputs); #endif for(i = 0; i < l.outputs; ++i){ printf("%f\n", l.output[i]); } /* printf("\n\nWeights\n"); for(i = 0; i < l.n*l.size*l.size*l.c; ++i){ printf("%f\n", l.filters[i]); } printf("\n\nBiases\n"); for(i = 0; i < l.n; ++i){ printf("%f\n", l.biases[i]); } */ top_predictions(net, top, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < top; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } free_image(im); if (filename) break; } }
void validate_recall(char *cfgfile, char *weightfile) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); srand(time(0)); char *val_images = "/home/pjreddie/data/voc/test/2007_test.txt"; list *plist = get_paths(val_images); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n - 1]; int num_boxes = l.side; int num = l.n; int classes = l.classes; int j; box *boxes = calloc(num_boxes*num_boxes*num, sizeof(box)); float **probs = calloc(num_boxes*num_boxes*num, sizeof(float *)); for(j = 0; j < num_boxes*num_boxes*num; ++j) probs[j] = calloc(classes+1, sizeof(float *)); int N = plist->size; int i=0; int k; float iou_thresh = .5; float thresh = .1; int total = 0; int correct = 0; float avg_iou = 0; int nms = 1; int proposals = 0; int save = 1; for (i = 0; i < N; ++i) { char *path = paths[i]; image orig = load_image_color(path, 0, 0); image resized = resize_image(orig, net.w, net.h); float *X = resized.data; float *predictions = network_predict(net, X); get_boxes(predictions+1+classes, num, num_boxes, 5+classes, boxes); get_probs(predictions, num*num_boxes*num_boxes, classes, 5+classes, probs); if (nms) do_nms(boxes, probs, num*num_boxes*num_boxes, (classes>0) ? classes : 1, iou_thresh); char *labelpath = find_replace(path, "images", "labels"); labelpath = find_replace(labelpath, "JPEGImages", "labels"); labelpath = find_replace(labelpath, ".jpg", ".txt"); labelpath = find_replace(labelpath, ".JPEG", ".txt"); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); for(k = 0; k < num_boxes*num_boxes*num; ++k){ if(probs[k][0] > thresh){ ++proposals; if(save){ char buff[256]; sprintf(buff, "/data/extracted/nms_preds/%d", proposals); int dx = (boxes[k].x - boxes[k].w/2) * orig.w; int dy = (boxes[k].y - boxes[k].h/2) * orig.h; int w = boxes[k].w * orig.w; int h = boxes[k].h * orig.h; image cropped = crop_image(orig, dx, dy, w, h); image sized = resize_image(cropped, 224, 224); #ifdef OPENCV save_image_jpg(sized, buff); #endif free_image(sized); free_image(cropped); sprintf(buff, "/data/extracted/nms_pred_boxes/%d.txt", proposals); char *im_id = basecfg(path); FILE *fp = fopen(buff, "w"); fprintf(fp, "%s %d %d %d %d\n", im_id, dx, dy, dx+w, dy+h); fclose(fp); free(im_id); } } } for (j = 0; j < num_labels; ++j) { ++total; box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float best_iou = 0; for(k = 0; k < num_boxes*num_boxes*num; ++k){ float iou = box_iou(boxes[k], t); if(probs[k][0] > thresh && iou > best_iou){ best_iou = iou; } } avg_iou += best_iou; if(best_iou > iou_thresh){ ++correct; } } free(truth); free_image(orig); free_image(resized); fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total); } }
void extract_boxes(char *cfgfile, char *weightfile) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); srand(time(0)); char *val_images = "/home/pjreddie/data/voc/test/train.txt"; list *plist = get_paths(val_images); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n - 1]; int num_boxes = l.side; int num = l.n; int classes = l.classes; int j; box *boxes = calloc(num_boxes*num_boxes*num, sizeof(box)); float **probs = calloc(num_boxes*num_boxes*num, sizeof(float *)); for(j = 0; j < num_boxes*num_boxes*num; ++j) probs[j] = calloc(classes+1, sizeof(float *)); int N = plist->size; int i=0; int k; int count = 0; float iou_thresh = .3; for (i = 0; i < N; ++i) { fprintf(stderr, "%5d %5d\n", i, count); char *path = paths[i]; image orig = load_image_color(path, 0, 0); image resized = resize_image(orig, net.w, net.h); float *X = resized.data; float *predictions = network_predict(net, X); get_boxes(predictions+1+classes, num, num_boxes, 5+classes, boxes); get_probs(predictions, num*num_boxes*num_boxes, classes, 5+classes, probs); char *labelpath = find_replace(path, "images", "labels"); labelpath = find_replace(labelpath, "JPEGImages", "labels"); labelpath = find_replace(labelpath, ".jpg", ".txt"); labelpath = find_replace(labelpath, ".JPEG", ".txt"); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); FILE *label = stdin; for(k = 0; k < num_boxes*num_boxes*num; ++k){ int overlaps = 0; for (j = 0; j < num_labels; ++j) { box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float iou = box_iou(boxes[k], t); if (iou > iou_thresh){ if (!overlaps) { char buff[256]; sprintf(buff, "/data/extracted/labels/%d.txt", count); label = fopen(buff, "w"); overlaps = 1; } fprintf(label, "%d %f\n", truth[j].id, iou); } } if (overlaps) { char buff[256]; sprintf(buff, "/data/extracted/imgs/%d", count++); int dx = (boxes[k].x - boxes[k].w/2) * orig.w; int dy = (boxes[k].y - boxes[k].h/2) * orig.h; int w = boxes[k].w * orig.w; int h = boxes[k].h * orig.h; image cropped = crop_image(orig, dx, dy, w, h); image sized = resize_image(cropped, 224, 224); #ifdef OPENCV save_image_jpg(sized, buff); #endif free_image(sized); free_image(cropped); fclose(label); } } free(truth); free_image(orig); free_image(resized); } }
void optimize_picture(network *net, image orig, int max_layer, float scale, float rate, float thresh, int norm) { //scale_image(orig, 2); //translate_image(orig, -1); net->n = max_layer + 1; int dx = rand()%16 - 8; int dy = rand()%16 - 8; int flip = rand()%2; image crop = crop_image(orig, dx, dy, orig.w, orig.h); image im = resize_image(crop, (int)(orig.w * scale), (int)(orig.h * scale)); if(flip) flip_image(im); resize_network(net, im.w, im.h); layer_t last = net->layers[net->n-1]; //net->layers[net->n - 1].activation = LINEAR; image delta = make_image(im.w, im.h, im.c); NETWORK_STATE(state); #ifdef GPU state.input = cuda_make_array(im.data, im.w*im.h*im.c); state.delta = cuda_make_array(im.data, im.w*im.h*im.c); forward_network_gpu(*net, state); copy_ongpu(last.outputs, last.output_gpu, 1, last.delta_gpu, 1); cuda_pull_array(last.delta_gpu, last.delta, last.outputs); calculate_loss(last.delta, last.delta, last.outputs, thresh); cuda_push_array(last.delta_gpu, last.delta, last.outputs); backward_network_gpu(*net, state); cuda_pull_array(state.delta, delta.data, im.w*im.h*im.c); cuda_free(state.input); cuda_free(state.delta); #else state.input = im.data; state.delta = delta.data; forward_network(*net, state); fltcpy(last.delta, last.output, last.outputs); calculate_loss(last.output, last.delta, last.outputs, thresh); backward_network(*net, state); #endif if(flip) flip_image(delta); //normalize_array(delta.data, delta.w*delta.h*delta.c); image resized = resize_image(delta, orig.w, orig.h); image out = crop_image(resized, -dx, -dy, orig.w, orig.h); /* image g = grayscale_image(out); free_image(out); out = g; */ //rate = rate / abs_mean(out.data, out.w*out.h*out.c); if(norm) normalize_array(out.data, out.w*out.h*out.c); fltaddmul(orig.data, out.data, orig.w * orig.h * orig.c, rate); /* normalize_array(orig.data, orig.w*orig.h*orig.c); scale_image(orig, sqrt(var)); translate_image(orig, mean); */ //translate_image(orig, 1); //scale_image(orig, .5); //normalize_image(orig); constrain_image(orig); free_image(crop); free_image(im); free_image(delta); free_image(resized); free_image(out); }
/* * BEE::Sprite::crop_image_width() - Crop the image to the given width * @new_crop_width: the new width to crop the image to */ int BEE::Sprite::crop_image_width(int new_crop_width) { crop.w = new_crop_width; // Set the width return crop_image(crop); // Return the status of the cropping }