Function not working, Syntax errors and more

Issue

The other day I was working on a project for an Image captioning model on Keras. But when I am running it, I am facing a host of error. Note that I am using Atom Editor and a virtual environment in Python, Running everything from a Command-line.

train_features = load_photo_features(os.path('C:/Users/neelg/Documents/Atom_projects/Main/features.pkl'), train)

In this line, I am receiving this error==>

  File "C:\Users\neelg\Documents\Atom_projects\Main\Img_cap.py", line 143
train_features = load_photo_features(os.path('C:/Users/neelg/Documents/Atom_projects/Main/features.pkl'), train)
             ^

SyntaxError: invalid syntax

I think that the syntax is correct regarding the function, yet the error persists. So, in a seperate file I copied the function and tried to isolate problem.
Code for the standalone function:-

from pickle import load
import os
def load_photo_features(filename, dataset):
    all_features = load(open(filename, 'rb'))
    features = {k: all_features[k] for k in dataset}
    return features

filename = 'C:/Users/neelg/Documents/Atom_projects/Main/Flickr8k_text/Flickr8k.trainImages.txt'

train_features = load_photo_features(os.path('C:/Users/neelg/Documents/Atom_projects/Main/features.pkl'), train)

Now, A different type of problem crops up:

Traceback (most recent call last):
  File "C:\Users\neelg\Documents\Atom_projects\Main\testing.py", line 10, in <module>
    train_features = load_photo_features(os.path('C:/Users/neelg/Documents/Atom_projects/Main/features.pkl'), train)
TypeError: 'module' object is not callable

Any help? I am trying to import the Flickr_8k dataset, which contains random pictures and another small dataset which are the labels of those photographs…

P.S=>Pls send suggestions after testing the code on tour own editors before submitting because I suspect there is some core problem arising due to the System encoding(As suggested by some others). Also, it is not possible to load the whole code due to it’s length and requirement of multiple files.

Solution

I was about to ask you the same question with @ted why do you use os.path when you are trying to load the file.

Normally, I am using the following code for loading from pickle:

def load_obj(filename):
  with open(filename, "rb") as fp:
      return pickle.load(fp, enconding = 'bytes')

Furthermore, if I try something like that it works:

from pickle import load
import os
import pdb

def load_photo_features(filename):
   all_features = load(open(filename, 'rb'))
   pdb.set_trace()

   #features = {k: all_features[k] for k in dataset}
   #return features

train_features = load_photo_features('train.pkl')

I do not know what is the dataset input to proceed, but loading of the pickle file works fine.

Answered By – Chris Tosh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published