Beispiel #1
0
def build_model(classes = 2):
  inputs = Input(shape = (IMAGE_SIZE, IMAGE_SIZE, 3))
  x = preprocess_input(inputs)
  x = NASNetMobile(weights=None, classes=classes)(x)
  model = Model(inputs=inputs, outputs=x)
  model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
  return model
 def model_evaluate(self, img_path):
     self.model = load_model(self.MODEL_DIR, compile=False)
     process_image = preprocess_input(cv2.imread(img_path))
     prediction = self.model.predict(np.expand_dims(process_image, axis=0))
     self.Line_Dat_Edit.neg_sample_ledit.setText(
         str(round(np.around(prediction, 2)[0][0] * 100, 2)) + '%')
     self.Line_Dat_Edit.pos_sample_ledit.setText(
         str(round(np.around(1 - prediction, 2)[0][0] * 100, 2)) + '%')
Beispiel #3
0
def load_image(path, preprocess=True):
    """Load and preprocess image."""
    x = image.load_img(path, target_size=(IMG_HEIGHT, IMG_WIDTH))
    if preprocess:
        x = image.img_to_array(x)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
    return x
Beispiel #4
0
def NASNetMobile(image_bytes):

    image_batch = np.expand_dims(image_bytes, axis=0)
    processed_imgs = nasnet.preprocess_input(image_batch)
    nasnet_features = nasnet_extractor.predict(processed_imgs)
    flattened_features = nasnet_features.flatten()
    # normalized_features = flattened_features / norm(flattened_features)
    return flattened_features
    def predict(self, image):
        img = preprocess_input(image)
        preds = decode_predictions(self.model.predict(img), 10)[0]

        results = {}
        for pred in preds:
            results.update({pred[1]: f"{pred[2] * 100:.2f}%"})

        return results
Beispiel #6
0
def data_gen(list_files, batch_size):
    while True:
        shuffle(list_files)
        for batch in chunker(list_files, batch_size):
            X = [read_image(x) for x in batch]
            Y = [get_class_from_file_path(x) for x in batch]

            X = [preprocess_input(x) for x in X]

            yield np.array(X), np.array(Y)
def getdata(x, y, batch=100):
    idx = np.random.randint(0, x.shape[0], size=batch)
    orix, batch_x, batch_y = [], [], y[idx]
    for p in x[idx]:
        img = load_img(p, target_size=(224, 224))
        img_np = np.array(img)
        orix.append(img_np)
        img_pre = nasnet.preprocess_input(img_np)  # transfer learning 用他的預處理方式
        batch_x.append(img_pre)
    return (np.array(orix), np.array(batch_x), np.array(batch_y))
Beispiel #8
0
def nasnet_feature_extractor(image_bytes):
    preprocess_image = nn_image_preprocessing(image_bytes)
    processed_imgs = nasnet.preprocess_input(preprocess_image)
    # predicting the image
    inception_resnet_v2_features =nasNet.predict(processed_imgs)
    # making features flatten and reshape
    flattened_features = inception_resnet_v2_features.flatten()
    flattened_features = np.array(flattened_features)
    flattened_features = flattened_features.reshape(1, -1)
    return flattened_features
Beispiel #9
0
def load_images(image_list, preprocess=True, target_size=(224, 224)):
    images = []
    for i in image_list:
        c_img = np.expand_dims(image.img_to_array(
            image.load_img(i, target_size=target_size)), axis=0)
        images.append(c_img)
    images = np.vstack(images)
    if preprocess:
        images = preprocess_input(images)
    return images
Beispiel #10
0
def nasnet_feature_extractor(preprocess_image):
    # preprocessing for image input the vgg_server
    # preprocess_image = nn_image_preprocessing(image_bytes)
    processed_imgs = nasnet.preprocess_input(preprocess_image)
    # predicting the image
    inception_resnet_v2_features = nasNet.predict(processed_imgs)
    # making features flatten and reshape
    flattened_features = inception_resnet_v2_features.flatten()
    flattened_features = np.array(flattened_features)
    flattened_features = flattened_features.reshape(1, -1)
    return sparse.csr_matrix(flattened_features)
Beispiel #11
0
def extract_features(img):
    """Extract features from input image and return the same"""
    print(f'>>> Entered feature extractor...', end='')
    start = time.time()
    img = image.load_img(img, target_size=(331, 331))
    # img = image.load_img(img, target_size=(299, 299))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    features = nas_model.predict(x)
    print(f'Feature extraction took: {time.time()-start:.2}. Exiting. ')
    return features.flatten()
Beispiel #12
0
    def evaluate(self, images):
        dbg("Evaluating the model on {} entries".format(len(images)))

        results = {}
        for img in images:
            try:
                image = cv2.imread(img)
                image = cv2.resize(image, (128, 128), 3)
            except:
                print(f"this image: {img} is no valid, ommiting..")
                continue

            image = preprocess_input(image)
            image = np.expand_dims(image, axis=0)
            results[img] = self.model.predict(image)
        return results
 def get_model_image(self):
     read_image = cv2.imread(self.raw_file)
     read_image = cv2.cvtColor(read_image, cv2.COLOR_BGR2RGB)
     process_image = preprocess_input(read_image)
     explainer = lime_image.LimeImageExplainer()
     explanation = explainer.explain_instance(
         np.array(process_image),
         self.model.predict,
         top_labels=10,
         hide_color=0,
         num_samples=10,
         segmentation_fn=SegmentationAlgorithm('felzenszwalb'))
     temp, mask = explanation.get_image_and_mask(explanation.top_labels[0],
                                                 positive_only=False,
                                                 num_features=10,
                                                 hide_rest=True)
     model_image = mark_boundaries(temp / 2 + 0.5, mask)
     self.getCanvas(model_image)
Beispiel #14
0
    def evaluate(self, folder_path):
        images = glob(folder_path + "/*.tif")
        print(images)
        dbg("Evaluating the model on {} entries".format(len(images)))

        results = {}
        i = 0
        for img in images:
            image = preprocess_input(cv2.imread(img))
            results[img] = self.model.predict(np.expand_dims(image, axis=0))
            # image = cv2.imread(img)
            # image = cv2.resize(image, (96, 96, 3))
            # image = preprocess_input(cv2.imread(img))
            # results[img] = self.model.predict(image)
            i += 1
            if i > 100:
                break
        return results
Beispiel #15
0
    def prepare_dataset(self):
        dbg("Preparing dataset")
        with tf.python_io.TFRecordWriter(self.tf_record_file_name) as writer:
            for i, entry in enumerate(
                    tqdm(self.images, total=self.dataset_size)):
                # load and preprocess
                image_data = cv2.imread(entry)
                image_data = cv2.resize(image_data, (128, 128), 3)
                image_data = preprocess_input(image_data)

                # cut path and extention to get id
                image_id = os.path.basename(entry)  #.replace(".jpg", "")
                try:
                    label = self.labels[image_id]
                except KeyError:
                    print(f'{image_id} does not exisit')
                    continue
                data = {
                    "image_data":
                    tf.train.Feature(bytes_list=tf.train.BytesList(
                        value=[image_data.tostring()])),
                    "label":
                    tf.train.Feature(int64_list=tf.train.Int64List(
                        value=label)),
                }
                # Cast to TensorFlow Features.
                feature = tf.train.Features(feature=data)
                # Cast as a TensorFlow Example.
                example = tf.train.Example(features=feature)
                # Serialize the data.
                serialized = example.SerializeToString()
                # Write the serialized data to the TFRecords file.
                writer.write(serialized)
                # if we reach desired dataset size, break preparation
                if i == self.dataset_size:
                    break
        dbg(f"Dataset file ({self.tf_record_file_name}) ready")
height = len(false_img) // width + 1
for i in range(len(false_img)):
    plt.subplot(height, width, i + 1)
    title = "[O]:{}\n[P]:{}".format(trans[false_label[i]], trans[false_pre[i]])
    plt.title(title)
    plt.axis("off")
    plt.imshow(false_img[i])

# pip install pillow
import PIL
import requests
url = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSOE_6cCV1OCLhwBAC5eo8MWtUKl7ye0YMb9A&usqp=CAU"
h = {
    "user-agent":
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
}
response = requests.get(url, stream=True, verify=False, headers=h)
img = PIL.Image.open(response.raw).resize((224, 224))
img_np = np.array(img)
test = nasnet.preprocess_input(img_np.reshape(1, 224, 224, 3))
probs = model.predict(test)[0]
for i, p in enumerate(probs):
    print(trans[i], "的機率是:", round(p, 3))

ans = model.predict(test)[0]
if ans[0] >= ans[1]:
    ans = 0
else:
    ans = 1
print("應該是:", trans[ans])
plt.imshow(img_np)
Beispiel #17
0
# grab the list of images in our dataset directory, then initialize
# the list of data (i.e., images) and class images
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset"]))
data = []
labels = []

# loop over the image paths
for imagePath in imagePaths:
    # extract the class label from the filename
    label = imagePath.split(os.path.sep)[-2]

    # load the input image (224x224) and preprocess it
    image = load_img(imagePath, target_size=(224, 224))
    image = img_to_array(image)
    image = preprocess_input(image)  #Convert to the range [-1, 1]

    # update the data and labels lists, respectively
    data.append(image)
    labels.append(label)

# convert the data and labels to NumPy arrays
data = np.array(data, dtype="float32")
labels = np.array(labels)

# perform one-hot encoding on the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)

# partition the data into training and testing splits using 75% of
input_tensor_name = input_names[0] + ":0"
output_tensor_name = output_names[0] + ":0"

#print("input_tensor_name: {}\noutput_tensor_name: {}".format(input_tensor_name, output_tensor_name))

output_tensor = tf_sess.graph.get_tensor_by_name(output_tensor_name)

# In[9]:

# Optional image to test model prediction.
img_path = '../../data/elephant.jpg'

img = image.load_img(img_path, target_size=image_size[:2])
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

feed_dict = {input_tensor_name: x}

#preds = tf_sess.run(output_tensor, feed_dict)

# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
#print('Predicted:', decode_predictions(preds, top=3)[0])

# In[10]:

import time
# in order to get rid of some unreletable values, cuz the first ones are always slow
for i in range(0, 5):
    one_prediction = tf_sess.run(output_tensor, feed_dict)
# OneHotEncoding
from sklearn.preprocessing import OneHotEncoder

y_train = y_train.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)

ohencoder = OneHotEncoder()
ohencoder.fit(y_train)
y_train = ohencoder.transform(y_train).toarray()
y_test = ohencoder.transform(y_test).toarray()

vgg19 = NASNetMobile(weights='imagenet',
                     include_top=False,
                     input_shape=(32, 32, 3))
vgg19.trainable = True
x_train = preprocess_input(x_train)
x_test = preprocess_input(x_test)

x_train = x_train.astype('float32') / 255.  # 전처리
x_test = x_test.astype('float32') / 255.  # 전처리

model = Sequential()
model.add(vgg19)
model.add(Flatten())
model.add(Dense(128))
model.add(Dense(64))
model.add(Dense(10, activation='softmax'))
model.summary()

from tensorflow.keras.optimizers import Adam
Beispiel #20
0
 def single_evaluate(self, img_path):
     input_data = preprocess_input(cv2.imread(img_path))
     return {
         img_path: self.model.predict(np.expand_dims(input_data, axis=0))
     }