【问题标题】:Spot the difference between these two images找出这两个图像之间的区别
【发布时间】:2018-11-12 00:31:32
【问题描述】:

以编程方式,我的代码正在检测两类图像之间的差异,并且始终拒绝一类,而始终允许另一类。

我还没有发现产生错误的图像和不产生错误的图像之间有什么区别。但是必须有一些区别,因为产生错误的那些在 100% 的时间内都会这样做,而其他的则在 100% 的时间内按预期工作。

特别是,我检查了两组颜色格式:RGB;尺寸:无显着差异;数据类型:uint8 在两者中;像素值的大小:两者相似。

下面是两张永远无法使用的图片,然后是两张始终可以使用的图片:

我怎样才能发现差异?

场景是我使用 Firebase 和 Swift iOS 前端将这些图像发送到 Google Cloud ML 引擎托管的 convnet。有些图像一直有效,而另一些图像则永远不会像上面那样工作。此外,当我使用 gcloud 版本 predict CLI 时,所有图像都有效。对我来说,问题必然是图像中的某些东西。因此,我在这里为成像组发帖。出于完整性的要求,代码已包含在内。

index.js 文件代码包含:

'use strict';

const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage');
const admin = require('firebase-admin');
const exec = require('child_process').exec;
const path = require('path');
const fs = require('fs');
const google = require('googleapis');
const sizeOf = require('image-size');

admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const rtdb = admin.database();
const dbRef = rtdb.ref();

function cmlePredict(b64img) {
    return new Promise((resolve, reject) => {
        google.auth.getApplicationDefault(function (err, authClient) {
            if (err) {
                reject(err);
            }
            if (authClient.createScopedRequired && authClient.createScopedRequired()) {
                authClient = authClient.createScoped([
                    'https://www.googleapis.com/auth/cloud-platform'
                ]);
            }

        var ml = google.ml({
           version: 'v1'
        });

        const params = {
            auth: authClient,
            name: 'projects/myproject-18865/models/my_model',
            resource: {
                instances: [
                {
                    "image_bytes": {
                    "b64": b64img
                    }
                }
                ]
            }
        };

        ml.projects.predict(params, (err, result) => {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });
});
}

function resizeImg(filepath) {
    return new Promise((resolve, reject) => {
        exec(`convert ${filepath} -resize 224x ${filepath}`, (err) => {
          if (err) {
            console.error('Failed to resize image', err);
            reject(err);
          } else {
            console.log('resized image successfully');
            resolve(filepath);
          }
        });
      });
}

exports.runPrediction = functions.storage.object().onChange((event) => {

    fs.rmdir('./tmp/', (err) => {
        if (err) {
            console.log('error deleting tmp/ dir');
        }
    });

const object = event.data;
const fileBucket = object.bucket;
const filePath = object.name;
const bucket = gcs().bucket(fileBucket);
const fileName = path.basename(filePath);
const file = bucket.file(filePath);

if (filePath.startsWith('images/')) {  
    const destination = '/tmp/' + fileName;
    console.log('got a new image', filePath);
    return file.download({
        destination: destination
    }).then(() => {
        if(sizeOf(destination).width > 224) {
            console.log('scaling image down...');
            return resizeImg(destination);
        } else {
            return destination;
        }
    }).then(() => {
        console.log('base64 encoding image...');
        let bitmap = fs.readFileSync(destination);
        return new Buffer(bitmap).toString('base64');
    }).then((b64string) => {
        console.log('sending image to CMLE...');
        return cmlePredict(b64string);
    }).then((result) => {
        console.log(`results just returned and is: ${result}`);  



        let predict_proba = result.predictions[0]

        const res_pred_val = Object.keys(predict_proba).map(k => predict_proba[k])
        const res_val = Object.keys(result).map(k => result[k])

        const class_proba = [1-res_pred_val,res_pred_val]
        const opera_proba_init = 1-res_pred_val
        const capitol_proba_init = res_pred_val-0

        // convert fraction double to percentage int
        let opera_proba = (Math.floor((opera_proba_init.toFixed(2))*100))|0
        let capitol_proba = (Math.floor((capitol_proba_init.toFixed(2))*100))|0
        let feature_list = ["houses", "trees"]


        let outlinedImgPath = '';
        let imageRef = db.collection('predicted_images').doc(filePath.slice(7));
               outlinedImgPath = `outlined_img/${filePath.slice(7)}`;
               imageRef.set({
                image_path: outlinedImgPath,
                opera_proba: opera_proba,
                capitol_proba: capitol_proba
            });

        let predRef = dbRef.child("prediction_categories");
        let arrayRef = dbRef.child("prediction_array");

    predRef.set({
            opera_proba: opera_proba,
            capitol_proba: capitol_proba,
            });

       arrayRef.set({first: {
           array_proba: [opera_proba,capitol_proba],
           brief_description: ["a","b"],
           more_details: ["aaaa","bbbb"],
           feature_list: feature_list},
        zummy1: "",
        zummy2: ""});

        return bucket.upload(destination, {destination: outlinedImgPath});

    });
} else {
    return 'not a new image';
}
}); 

【问题讨论】:

  • 很明显我给你的 IQ 分数比你愿意接受的要多,Chris -- j/k。更严重的是,我已经缩小了对上述解决方案的搜索范围,我需要这里的帮助。除了我已经排除的属性之外,还有哪些其他图像属性可能在算法上对这些图像进行分类的任何线索?谢谢!!!
  • 好的,我现在将编辑以包含代码。场景是我使用带有 swift iOS 前端的 firebase 将这些图像发送到谷歌云 ML 引擎托管的 convnet。有些图像一直有效,而另一些图像则永远不会像上面那样工作。此外,当我使用 gcloud 版本 predict CLI 时,所有图像都有效。对我来说,问题必然是图像中的某些东西。我在这里向你们发布了在图像属性方面比我更聪明、更有经验的成像专家。
  • 您收到的任何具体错误消息?
  • 它将输入调整为预期的大小。我将尝试将“拒绝”图像之一的大小更改为“接受”图像之一的大小,然后看看会发生什么。
  • 是的,这是错误:msg >TypeError: Cannot read property '0' of undefined at file.download.then.then.then.then (/user_code/index.js:128:51)在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

标签: image image-processing jpeg python-imaging-library imaging


【解决方案1】:

问题是坏图像是灰度的,而不是我的模型所期望的 RGB。我最初是通过查看形状来检查的。但是“坏”图像有 3 个颜色通道,这 3 个通道中的每一个都存储相同的数字——所以我的模型拒绝接受它们。此外,正如预期的那样,与我最初观察到的相反,gcloud ML-engine predict CLI 实际上对于这些图像也失败了。我花了 2 天时间才弄清楚这一点!

【讨论】:

    猜你喜欢
    • 2014-11-04
    • 2014-06-02
    • 2018-06-12
    • 2011-09-19
    • 1970-01-01
    • 2020-11-19
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多