-2

I only have around 1000 images of computers. I need to train a model that can identify if the image is computer or not-computer. I do not have a dataset for not-computer, as it could be anything.

I guess the best method for this would be to apply transfer learning. I am trying to train data on a pre-trained VGG19 Model. But still, I am unaware on how to train a model with just computers images without any non-computer images.

I am new to ML Overall, so sorry if question is not to the point.

Bishal Th.
  • 60
  • 9
  • 1
    This topic is unique in the sense that the question is off-topic (not a programming quedtion and must be asked on se/datascience or se/crossvalidated) and all answers must be deleted because they are all comments/opinions but not solutions to a well-defined question. – FatihAkici Nov 27 '19 at 19:40
  • 1
    There are two things you can do: 1. Collect data on non-computers and build your binary classification model. 2. Implement a one-class classification model. Summary of the garbage answers posted below. – FatihAkici Nov 27 '19 at 19:51

4 Answers4

1

No way, I'm sorry. You'll need a lot (at least other 1000 images) of non-computer images. You can take them from everywhere, the more they "vary" the better is for your model to extract what features characterize a computer.

Imagine to be a baby that is trained to always say "yes" in front of something, next time you'll se something you'll say "yes" no matter what is in front of you...

The same is for machine learning models, you need positive examples and negative examples, or your model will have 100% accuracy by predicting always "yes".

If you want to see it a mathematically/geometrically, you can see each sample (in your case an image) as a point in the feature space: imagine to draw an axis for each attribute you have (x,y,z an so on), an image will be a point in that space.

For simplicity let's consider a 2-dimension space, which means that each image could be described with 2 attributes (not the case for images, usually the features are a lot, but for simplicity imagine feature_1 = number of colors, feature_2 = number of angles), in this example we can simply draw a point in a cartesian graph, one for each image:

Imagine red points are computer images, while blue are non-computer

The objective of a classifier is to draw a line which better separate the red dots from the blue dots, which means separate positive examples, from negative examples.

If you give the model only positive samples (which is what you were going to do), you'll have infinite models with 100% accuracy! Because you can put a line wherever you want, the only requirement is to not "cut" your dataset.

Given that I suppose you are a beginner, I'll just tell you what to do, not how because it would take years ;)

1) Collect data - as I told you, even negative examples, at least other 1000 samples

2) Split the data into train/test - a good split could be 2/3 of the samples in the training set and 1/3 in the test set. [REMEMBER] Keep consistency of the final class distribution, i.e. if you had 50%-50% of classes "Computer"-"Non computer", you should keep that percentage for both train set and test set

3) Train a model - have a look at this link for a guided examples, it uses the MNIST dataset, which is a famous image classification one, you should use your data

4) Test the model on the test set and look at performance

1

While it is not impossible to take data belonging to one only one class of data and then use methods to classify whether other data belong to the same class or not, you usually do not end up with too good accuracy that way.

One way to do this, is to use something called "autoencoders". The point here is that you use the same image as input and as the target, and you make sure that the (usually neural network) is forced to compress the image in some way so that it only stores what is important to recreate images of computers. Ideally, this should lead to a model which is good at recreating images of computers, and bad at everything else, meaning you can test how high the loss is on the output, and if it higher than some threshold you've decided on, you deem it to be something else. Again, you're probably not going to get anything close to 90% accuracy doing this, but it is an approach to your problem.

A better approach is to go hunting for models which have been pre-trained on some dataset which had computers as part of the dataset, take the same dataset and set all computers to one class (+ your own images, make sure they adhere to the dataset format) and a selection of the other images to the other class. Make sure to not make the classes too unbalanced, otherwise your model will suffer from it. Extend the pre-trained model with a couple of layer, fully connected should probably do fine, and make the pre-trained part of the model not trainable, so you don't mess up the good weights there when you're practically telling it to ignore everything which is not a computer. This is probably your best bet, but is going to require a bit more effort on your side in terms of finding all of these parts which you need to make it happen, and to understand how to integrate that code into yours.

Kazesui
  • 159
  • 1
  • 4
  • This is a comment/opinion/suggestion, not a solution/answer. – FatihAkici Nov 27 '19 at 19:53
  • Stack Overflow considers questions about software algorithms to be appropriate. While the question does not specifically mention this, it can be reasonably be inferred based on the tags. Autoencoders can be considered a type of algorithm which fits the specified problem. Some opinions were tossed in for good measure, but if no opinions were to be allowed, we'd probably have to purge a lot of questions and answers. – Kazesui Nov 27 '19 at 20:33
  • Two issues: 1. There is no programming question on this thread. 2. It is 100% fine for an answer to have an opinion, that is the nature of answering a question, but your answer is 100% opinion and 0% code. Your contribution would be a perfect comment, but not an answer because it doesn't solve a problem, it just offers advice. Which is perfectly fine for comments but that's not what answers are for. – FatihAkici Nov 27 '19 at 20:39
  • 1
    [How to Answer](https://stackoverflow.com/help/how-to-answer) strongly recommends only answering well-asked questions. Please also see: [Should one advise on off-topic questions?](https://meta.stackoverflow.com/questions/276572/should-one-advise-on-off-topic-questions) – Andreas Nov 28 '19 at 01:15
1
  1. You can either use transfer learning using a pretrained model on the imagenet dataset. As mentioned in another answer, there are a bunch of classes inside imagenet close to computers and electronic devices (such as monitors, CD players, laptops, speakers, etc.). So you can fine-tune the model on your dataset and train it to predict computers (train on around 750 images and test on the remaining 250).
  2. You can manually collect images for objects other than computers, preferably a lot of electronic devices (because they are close to computers) and a bunch of other household things (there is a home objects dataset by Caltech). You should collect about 1000 such images to have a class balance. You can train your own custom model once you have this dataset.
Aditya Sharma
  • 331
  • 1
  • 3
  • 13
0

No problem!

step one: install a deep-learning toolkit of your choice. they all come with nice tutorials these days.

step two: grab a pre-trained imagenet model. In that model, there are already a few computer classes built into it! ( "desktop_computer", "laptop", 'notebook", and another class for hand-held computers "hand-held_computer")

step three: use model to predict. for this, you'll need to have your images the correct size.

more steps: further fine-tune the model...a bit more advanced but will give you some gains.

Something to think about is what is your goal? accuracy? false positives/negatives, etc? It's always good having a goal of what you need to accomplish from the start.

EDIT: probably the easiest way to get started(if you don't have libraries, gpu, etc) is to go to google colab ( https://colab.research.google.com/notebooks/welcome.ipynb ) and make a notebook in your browser and run the following code.

#some code take and modded from https://www.learnopencv.com/keras-tutorial-    using-pre-trained-imagenet-models/

import keras
import numpy as np
from keras.applications import vgg16

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO

%matplotlib inline
vgg_model = vgg16.VGG16(weights='imagenet')

def predict_image(image_url, model):

    response = requests.get(image_url)
    original = Image.open(BytesIO(response.content))

    newsize = (224, 224) 
    original = original.resize(newsize) 

    # convert the PIL image to a numpy array
    # IN PIL - image is in (width, height, channel)
    # In Numpy - image is in (height, width, channel)
    numpy_image = img_to_array(original)

    # Convert the image / images into batch format
    # expand_dims will add an extra dimension to the data at a particular axis
    # We want the input matrix to the network to be of the form (batchsize, height, width, channels)
    # Thus we add the extra dimension to the axis 0.
    image_batch = np.expand_dims(numpy_image, axis=0)
    plt.imshow(np.uint8(image_batch[0]))
    plt.show()

    # prepare the image for the VGG model
    processed_image = vgg16.preprocess_input(image_batch.copy())

    # get the predicted probabilities for each class
    predictions = model.predict(processed_image)

    # convert the probabilities to class labels
    # We will get top 5 predictions which is the default
    label = decode_predictions(predictions)
    print label[0][0:2]  #just display top 2


urls = ['https://4.imimg.com/data4/CO/YS/MY-29352968/samsung-desktop-computer-500x500.jpg', 'https://cdn.britannica.com/77/170477-050-1C747EE3/Laptop-computer.jpg']

for u in urls:
    predict_image(u, vgg_model)

images and predicted labels

This should be a good starting point. Oh, and if the top predicted label is not in the computer, laptop, etc set, then it's NOT a computer!

user1269942
  • 3,772
  • 23
  • 33
  • This is a comment/opinion/suggestion, not a solution/answer. – FatihAkici Nov 27 '19 at 19:53
  • it's a solution...one has to start somewhere. just run it yourself. – user1269942 Nov 27 '19 at 19:53
  • Solution to a non-question? The OP is just looking for general advice, the post must be closed, not answered. – FatihAkici Nov 27 '19 at 19:54
  • I voted to close the post and answers. – FatihAkici Nov 27 '19 at 19:57
  • Btw, you are not even answering the pseudo-question, you are answering something totally different. The person is asking how to train a model on a one-class data, your answer recommends using a pre-trained model to predict stuff. This is not even what s/he is looking for. – FatihAkici Nov 27 '19 at 19:58
  • 1
    I do agree with the process. I just didn't agree with the responses he got which were basically "no" or "need more skills" or "need more data". If they were indeed "asking" what I thought (which could be different than I interpreted, agreed!), I didn't want them leaving thinking it wasn't possible...so I squeezed in an answer with code to demonstrate it *may* be totally possible depending on what their goal is. I'm happy to have the whole thing removed/deleted/removed...I'm not attached. I just hope it was helpful. And I appreciate you removing the downvotes you gave. – user1269942 Nov 27 '19 at 21:46