ValueError: Cannot feed value of shape (1, 1) for Tensor 'Placeholder_765:0', which has shape '(1,)' - tensorflow

I have a trained an InceptionV3 from scratch on a custom dataset containing 100 classes. Initialized the CNN model on Keras. I am now trying to generate adversarial examples for this model of mine using Foolbox, however I am getting the above error. Where am I going wrong? The library(Foolbox) seems to be working fine for others and my model gets past the image classification process correctly without any error but the wrapper model generates it.
from keras.models import load_model
from keras.applications.vgg16 import VGG16
import foolbox
from foolbox.models import KerasModel
from foolbox.attacks import LBFGSAttack
from foolbox.criteria import TargetClass
import numpy as np
import foolbox
keras.backend.set_learning_phase(0)
model=load_model('standard_inceptionV3.h5')
fmodel = foolbox.models.KerasModel(model, bounds=(0, 255))
from PIL import Image
img = Image.open('/home/shikhar/Downloads/suit.jpeg')
img = img.resize((224,224))
img = np.asarray(img)
img = img[:, :, :3]
lab=model.predict(np.expand_dims(img, axis=0))
label=np.argmax(lab,axis=1)
from foolbox.criteria import Misclassification, TargetClass
attack = foolbox.attacks.FGSM(model=fmodel)
adversarial = attack(img, label,unpack=False)

Related

Lambda function in tensorflow to do some changes to the data inside the model

My goal is changing the data inside the model using lambda function
the code fails at the last part in model.predict
can someone help me fix this or give me a similar one if you have?
import glob
import tensorflow as tf
import tensorflow_hub as hub
from os.path import basename, join
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from keras.optimizers import Adam
from keras import layers
from sklearn.metrics import auc, average_precision_score
import numpy as np
import base64
import cv2
#this is the function that i want to call inside the model
#it take th data which in an image encoded base64 and it decode it back into a numpy array
def base64_decoder(inputs):
binary_data = base64.b64decode(inputs)
img = cv2.imdecode(np.frombuffer(binary_data, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
return img
#this is the model i'm using to test
model_handle="https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_s/feature_vector/2"
INPUT_SIZE=(331, 331,3)
model = tf.keras.Sequential([
tf.keras.layers.Lambda(base64_decoder),
tf.keras.layers.InputLayer(input_shape=INPUT_SIZE),
hub.KerasLayer(model_handle, trainable=True),
tf.keras.layers.Dropout(rate=0.2),
layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=[tf.keras.metrics.AUC(curve='PR')])
# now i will test the model
im = np.random.rand(331,331,3)
img = np.expand_dims(im, axis=0)
#encode the random image
_, buffer = cv2.imencode('.jpg', im)
jpg_as_text = base64.b64encode(buffer)
value=jpg_as_text.decode('utf-8')
#predict
print(model.predict(value))
also i may change the lambda function to a full custom function, what do you think?

Has the "ConvNeXt" family of models been removed from Keras?

When trying to use the ConvNeXtTiny model from Keras, I get the following error: AttributeError: module 'keras.applications' has no attribute 'ConvNeXtTiny'
filename = "ConvNextTiny_firstpass_model"
# layer construction
base_model = applications.ConvNeXtTiny( #preproccing included
input_shape=(targetWidth, targetHeight, 3),
include_top=False,
)
base_model.trainable = False
flatten_layer = layers.Flatten()
fc_layer = layers.Dense(1024, activation='relu')
dropout_layer = layers.Dropout(0.3)
#layer connecting
x = flip_layer(input_layer)
x = base_model(x, training=False)
x = flatten_layer(x)
x = fc_layer(x)
x = dropout_layer(x)
predictions = output_layer(x)
model = keras.Model(input_layer, predictions)
Here are my imports:
import tensorflow as tf
import keras
from keras import layers
from keras import optimizers
from keras import applications
from keras import losses
from keras import callbacks
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import cv2 as cv
import csv
from sklearn.utils import shuffle
Possibly relevant versioning:
ipython==8.5.0
tensorflow==2.10.0
keras==2.10.0
Keras-Preprocessing==1.1.2
pandas==1.4.4
numpy==1.23.3
matplotlib==3.6.0
opencv-python==4.6.0.66
sklearn==0.0
The previous imports placed above the convnext import were causing issues.
Moving from tensorflow.keras.applications import convnext to the top of all the imports allowed it to import properly.

Input 0 of layer fc1 is incompatible with the layer: expected axis -1 of input shape to have value 25088 but received input with shape (None, 32768)

I'm implementing SRGAN (and am not very experienced in this field), which uses a pre-trained VGG19 model to extract features. The following code was working fine on Keras 2.1.2 and tf 1.15.0 till yesterday. then it started throwing an "AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'" So i updated the keras version to 2.4.3 and tf to 2.5.0. but then its showing a "Input 0 of layer fc1 is incompatible with the layer: expected axis -1 of input shape to have value 25088 but received input with shape (None, 32768)" on the following line
features = vgg(input_layer)
But here the input has to be (256,256,3).
I had downgraded the keras and tf versions to the one I mentioned before to get rid of this error in the first place and it was working well till yesterday.
changing the input shape to (224,224,3) does not work. Any help in solving this error will be very appreciated.
import glob
import time
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import keras
from keras.layers import Input
from keras.applications.vgg19 import VGG19
from keras.callbacks import TensorBoard
from keras.layers import BatchNormalization, Activation, LeakyReLU, Add, Dense,Flatten
from keras.layers.convolutional import Conv2D, UpSampling2D
from keras.models import Model
from keras.optimizers import Adam
from scipy.misc import imread, imresize
from PIL import Image
def build_vgg():
input_shape = (256, 256, 3)
vgg = VGG19(weights="imagenet")
vgg.outputs = [vgg.layers[9].output]
input_layer = Input(shape=input_shape)
features = vgg(input_layer)
model = Model(inputs=[input_layer], outputs=[features])
return model
vgg = build_vgg()
vgg.trainable = False
vgg.compile(loss='mse', optimizer=common_optimizer, metrics=['accuracy'])
# Build and compile the discriminator
discriminator = build_discriminator()
discriminator.compile(loss='mse', optimizer=common_optimizer, metrics=['accuracy'])
# Build the generator network
generator = build_generator()
The Error message
Im using google colab
Importing keras from tensorflow and setting include_top=False in
vgg = VGG19(weights="imagenet",include_top=False)
seems to work.

TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.core.Dense object at 0x11f561b00>

I have a task where I am building a model and I am trying to use sequential but I ended up having an error. I am not sure what went wrong. This is just the start of the model but can't seem to work. To add more details the generator code is where I import the data and use it as an image generator. Deep code is code where I am building the model.
Deep Code:
import tensorflow as tf
from tensorflow import keras
import numpy as np
print(tf.__version__)
from generators import ImageGenerator
imagegenerator = ImageGenerator(3)
model = keras.Sequential()
model.add(Dense(32,activation='relu', input_shape=()))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit_generator(imagegenerator,epochs=20)
generator code:
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import numpy as np
import string, random
import keras
import abc
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
-----

How to get the bounding box coordinates for the detected object

I am new to tensorflow.am very confusing to get the bounding box coordinates for the detected object.how to get the bounding box to the detected object.this is my code please help!!
from PIL import Image
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import os
import cv2
test_dir=os.getcwd()+'/test_img'
test_img=os.listdir(test_dir)
def convert_to_array(img):
im = cv2.imread(img)
img_ = Image.fromarray(im, 'RGB')
image = img_.resize((224,224))
return np.array(image)
def get_cell_name(label):
if label==0:
return "daisy"
if label==1:
return "dandelion"
model = tf.keras.experimental.load_from_saved_model('E:/model/flowers.h5', custom_objects={'KerasLayer':hub.KerasLayer})
model.build((None, 224, 224, 3))
for img in test_img:
file = os.path.join(test_dir, img)
ar=convert_to_array(file)
ar=ar/255
label=1
a=[]
a.append(ar)
a=np.array(a)
score=model.predict(a,verbose=1)
print(score)
label_index=np.argmax(score)
print(label_index)
acc=np.max(score)
Cell=get_cell_name(label_index)
print(Cell,acc)
img = cv2.imread(file)
text='this is '+Cell
cv2.putText(img,text, (5,15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 255)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
is you asking about this structure.
model = tf.keras.Sequential([
hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
trainable=do_fine_tuning),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(train_generator.num_classes, activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()```
You are trying to use a classification network. A classification network will classify an entire image into one of the classes.
For detecting objects in image, try networks such as RCNN, RetinaNet, Yolo etc.