AttributeError: 'DataFrame' object has no attribute '_data' [Not a duplicate]

1.1k Views Asked by At

I was trying to run the main.py but it threw an error about attribution.

My Python version is Python 3.5. I am using the CNTK Docker release 2.6-cpu-python3.5. I cannot update the Python version because of CNTK. It only supports Python 3.5 and will only run in Ubuntu 16.04.

Pandas version: pandas==0.25.3

The Error

Traceback (most recent call last):
  File "/workspace/main.py", line 5, in <module>
    from model import extract_patches, score_patch, del_cache
  File "/workspace/model.py", line 2, in <module>
    from regressionModel import extract_features, predict_label
  File "/workspace/regressionModel.py", line 26, in <module>
    regression_model = read_model['model'][0]
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/frame.py", line 2898, in __getitem__
    if self.columns.is_unique and key in self.columns:
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/generic.py", line 5063, in __getattr__
    return object.__getattribute__(self, name)
  File "pandas/_libs/properties.pyx", line 65, in pandas._libs.properties.AxisProperty.__get__
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/generic.py", line 5063, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute '_data'

main.py

import os
import flask
import numpy as np
from flask import jsonify, request
from model import extract_patches, score_patch, del_cache

app = flask.Flask(__name__)

@app.route('/url/<path:argument>')
def url(argument):
    # create a patch folder
    patch_path = './patches'
    if not os.path.exists(patch_path):
        os.mkdir(patch_path)
    
    # get image url from the query string
    imageURL = request.url.split('=',1)[1]
    
    # extract patches from imageURL
    dimension, face_loc, image_dim = extract_patches(imageURL)
    
    # score each patch
    patch_score= score_patch(patch_path)
    
    # delete the downloaded image and the patches from local
    del_cache(patch_path)
    if os.path.exists('temp.jpg'):
        os.remove('temp.jpg')
    
    data = dict()
    data['patch_score'] = []
    for key in dimension:
        tmp = []
        tmp[:] = dimension[key]
        tmp.append(patch_score[key])
        data['patch_score'].append(tmp)
   
    data['image_score'] = round(np.mean(list(patch_score.values())), 2) 
    data['face_loc'] = face_loc['face_loc']
    data['img_dim'] = image_dim

    return jsonify(patch_score = str(data['patch_score']), image_score = str(data['image_score']), face_loc = str(data['face_loc']), image_dim = str(data['img_dim']))

if __name__ == '__main__':
    app.run(host='0.0.0.0', port = 9580) # port number can be changed in your case

model.py

import getPatches
from regressionModel import extract_features, predict_label
import os
import shutil

def extract_patches(imageURL):
    patch_path = './patches'
    dimension_dict = dict()
    face_dict = dict()
    image_dim = []
    try:
        dim, face, img = getPatches.extract_patches(imageURL, dimension_dict,face_dict, image_dim, patch_path)
        print ("extract patches pass")
    except:
        print ('cannot extract patches from the image')
    return dim, face, img

def score_patch(patch_path):
    patch_score = dict()
    for file in next(os.walk(patch_path))[2]:
        file_path = os.path.join(patch_path, file)    
        score_features = extract_features (file_path)[0].flatten()# extract features from CNTK pretrained model      
        pred_score_label = predict_label(score_features) # score the extracted features using trained regression model
        patch_score[file.split('.')[0]] = float("{0:.2f}".format(pred_score_label[0]))
    return patch_score


def infer_label(patch_score, label_mapping):
    max_score_name, max_score_value = max(patch_score.items(), key=lambda x:x[1])
    pred_label = label_mapping[round(max_score_value)-1]
    return pred_label

def del_cache(patch_folder):
    shutil.rmtree(patch_folder)
    return

regressionModel.py

import numpy as np
import pandas as pd
import cntk as C
from PIL import Image
import pickle
from cntk import load_model, combine
import cntk.io.transforms as xforms
from cntk.logging import graph
from cntk.logging.graph import get_node_outputs


pretrained_model = 'ResNet152_ImageNet_Caffe.model'
pretrained_node_name = 'pool5'
regression_model = 'cntk_regression.dat'
image_width = 224
image_height = 224

# load CNTK pretrained model
#model_file  = os.path.join(pretrained_model_path, pretrained_model_name)
loaded_model  = load_model(pretrained_model) # a full path is required
node_in_graph = loaded_model.find_by_name(pretrained_node_name)
output_nodes  = combine([node_in_graph.owner])

# load the stored regression model
read_model = pd.read_pickle(regression_model)
regression_model = read_model['model'][0]
train_regression = pickle.loads(regression_model)

def extract_features(image_path):   
    img = Image.open(image_path)       
    resized = img.resize((image_width, image_height), Image.ANTIALIAS)  
    
    bgr_image = np.asarray(resized, dtype=np.float32)[..., [2, 1, 0]]    
    hwc_format = np.ascontiguousarray(np.rollaxis(bgr_image, 2)) 
    
    arguments = {loaded_model.arguments[0]: [hwc_format]}    
    output = output_nodes.eval(arguments)   
    return output

def predict_label(features):
    return train_regression.predict(features.reshape(1,-1))
1

There are 1 best solutions below

1
Itamar Turner-Trauring On

https://pypi.org/project/cntk/#files has CNTK 2.7 for Python 3.6. Still an obsolete version, but not quite as obsolete.