微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError:'str'对象没有属性'shape'ImageTextGenerator

如何解决AttributeError:'str'对象没有属性'shape'ImageTextGenerator

有人可以帮助我解决错误吗? 我想通过此链接https://github.com/AbhishekAnand18/ImageTextRecognition/blob/master/ImageTextRecognition_Code.ipynb重新创建图像文本识别 使用我自己的数据集。但是我想拟合模型时发现了来自data_generator类的错误。 我不知道怎么了,因为我只是按照Github中的说明进行操作。 错误看起来像这样:

error

这是我的数据生成器类:

class DataGenerator(keras.callbacks.Callback):
def __init__(self,img_dirpath,img_w,img_h,batch_size,n,output_labels,max_text_len=17):
    self.img_h = img_h                    #Image Height
    self.img_w = img_w                    #Image Width
    self.batch_size = batch_size          #Batch size of Input
    self.max_text_len = max_text_len      #Maximum Text length of Labels
    
    self.n=n
    self.img_dir = img_dirpath[:self.n]     # images list
    self.indexes = list(range(self.n))   #List of indices for each image in img_matrix
    self.cur_index = 0                   #Current index which points to image being loaded 
    self.imgs = np.zeros((self.n,self.img_h,self.img_w))
    self.texts =  output_labels[:self.n]                  #List of Ground Truth Label texts


def build_data(self):
    """
    Build The Image Data
    """
    print(self.n," Image Loading start...")
    for i,img_file in enumerate(self.img_dir):
        img = cv2.imread(img_file)
        img = img[:,:,1]                               #Extracting Single Channel Image
        img = cv2.resize(img,(self.img_w,self.img_h))
        img = img /255
        self.imgs[i,:]= img
        if i%10000==0:
            print("Loaded Images: ",i)
       
    print("Number of Texts matches with Total Number of Images :",len(self.texts) == self.n)
    print(self.n," Image Loading finish...")


def next_data(self): 
    """
    Returns image and text data pointed by the current index
    """
    self.cur_index += 1
    #If current index becomes more than the number of images,make current index 0 
    #and shuffle the indices list for random picking of image and text data
    if self.cur_index >= self.n:
        self.cur_index = 0
        random.shuffle(self.indexes)
    return self.imgs[self.indexes[self.cur_index]],self.texts[self.indexes[self.cur_index]]

def next_batch(self):
    """
    Creates a batch of images images and text data equal to the batch_size,computes the parameters needed for CTC and returns the inputs to the Model
    """
    while True:
        X_data = np.ones([self.batch_size,self.img_w,1])  #Single channel Gray Size Scale images for input
        #Initilizing with -1 to aid for padding labels of different lengths
        Y_data = np.ones([self.batch_size,self.max_text_len])* -1        #Text labels for input
       #input_length for CTC which is the number of time-steps of the RNN output
        input_length = np.ones((self.batch_size,1)) * 40
        label_length = np.zeros((self.batch_size,1))                   #label length for CTC
        source_str=[]                                                   #List to store Ground Truth Labels
        for i in range(self.batch_size):
            img,text = self.next_data() #getting the image and text data pointed by current index
                                #taking transpose of image
            img=img.T
            img = np.expand_dims(img,-1)  #expanding image to have a single channel
            X_data[i] = img
            label=encode_words_labels(text) # encoding label text to integer list and storing in temp label variable
            lbl_len=len(label)
            Y_data[i,0:lbl_len] = label #Storing the label till its length and padding others
            label_length[i] = len(label)
            source_str.append(text) #storing Ground Truth Labels which will be accessed as reference for calculating metrics
        
    #Preparing the input for the Model
        inputs = {
            'img_input': X_data,'ground_truth_labels': Y_data,'input_length': input_length,'label_length': label_length,'source_str': source_str  # used for visualization only
        }
        #Preparing output for the Model and intializing to zeros
        outputs = {'ctc': np.zeros([self.batch_size])}  
        yield (inputs,outputs) # Return the Prepared input and output to the Model,yield

我认为错误在于next_batch函数。但我不知道我看到next_batch函数返回的是字典。

我已经在Github中打开问题。你们可以看看吗? 非常感谢。 https://github.com/AbhishekAnand18/ImageTextRecognition/issues/1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。