Skip to content

Instantly share code, notes, and snippets.

@awjuliani
Last active September 14, 2021 20:52
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save awjuliani/5ce098b4b76244b7a9e3 to your computer and use it in GitHub Desktop.
Save awjuliani/5ce098b4b76244b7a9e3 to your computer and use it in GitHub Desktop.
A simple ipython notebook that walks through the creation of a softmax regression model using MNIST dataset.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rolfdstoll
Copy link

Hi,
unfortuately the code doesn't work for me.
The main loop [in 11] produces the folowing error: ValueError: row, column, and data arrays must be 1-D
(\python-3.5.2.amd64\lib\site-packages\scipy\sparse\coo.py in getnnz(self, axis)): line 195

any solution?

Cheers
Rolf

@mrunalilipte4
Copy link

I am working image caption generator project. now I am getting this kind of error in the testing phase.... hope someone will help me resolve this error.... THANK
Screenshot (221)
YOU!!!

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import argparse

ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True, help="C:\Users\Dell\anaconda3\envs\testenv\Flickr8k_Dataset\Flicker8k_Dataset\667626_18933d713e.jpg")
args = vars(ap.parse_args())
img_path = args['image']

def extract_features(filename, model):
try:
image = Image.open(filename)
except:
print("ERROR: Couldn't open image! Make sure the image path and extension is correct")
image = image.resize((299,299))
image = np.array(image)
# for images that has 4 channels, we convert them into 3 channels
if image.shape[2] == 4:
image = image[..., :3]
image = np.expand_dims(image, axis=0)
image = image/127.5
image = image - 1.0
feature = model.predict(image)
return feature

def word_for_id(integer, tokenizer):
for word, index in tokenizer.word_index.items():
if index == integer:
return word
return None

def generate_desc(model, tokenizer, photo, max_length):
in_text = 'start'
for i in range(max_length):
sequence = tokenizer.texts_to_sequences([in_text])[0]
sequence = pad_sequences([sequence], maxlen=max_length)
pred = model.predict([photo,sequence], verbose=0)
pred = np.argmax(pred)
word = word_for_id(pred, tokenizer)
if word is None:
break
in_text += ' ' + word
if word == 'end':
break
return in_text
#path = 'Flicker8k_Dataset/111537222_07e56d5a30.jpg'
max_length = 32
tokenizer = load(open("tokenizer.p","rb"))
model = load_model('models/model_9.h5')
xception_model = Xception(include_top=False, pooling="avg")
photo = extract_features(img_path, xception_model)
img = Image.open(img_path)
description = generate_desc(model, tokenizer, photo, max_length)
print("\n\n")
print(description)
plt.imshow(img)


usage: [-h] -i IMAGE
: error: the following arguments are required: -i/--image
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment