测试视频预测报错IndexError:数组索引需为整数/布尔类型求助
解决IndexError: arrays used as indices must be of integer (or boolean) type错误
问题场景
在为测试视频生成模型预测时触发以下错误:
IndexError: arrays used as indices must be of integer (or boolean) type
实现逻辑:
- 创建两个空列表存储预测标签与真实标签
- 遍历测试集中的每个视频,提取帧存入temp文件夹(每次迭代清空文件夹)
- 读取temp文件夹中的帧,用预训练模型提取特征,预测标签后取众数作为视频标签存入列表,同时存入真实标签
完整代码:
# creating two lists to store predicted and actual tags predict = [] actual = [] # for loop to extract frames from each test video count = 0 for i in tqdm(range(test_videos.shape[0])): videoFile = test_videos[i] cap = cv2.VideoCapture(test_videos[i]) # capturing the video from the given path frameRate = cap.get(cv2.CAP_PROP_FPS) #frame rate x=1 # removing all other files from the temp folder files = glob('E:/FCI Bio/GP/Dataset/Paper/3/3.1/OHP/Labeled_Dataset/videos/dataset/test/temp/*') for f in files: os.remove(f) while(cap.isOpened()): frameId = cap.get(1) #current frame number ret, frame = cap.read() if (ret != True): break if (frameId % math.floor(frameRate) == 0): # storing the frames of this particular video in temp folder filename ='E:/FCI Bio/GP/Dataset/Paper/3/3.1/OHP/Labeled_Dataset/videos/dataset/test/temp/' + os.path.basename(videoFile).split('_')[0] + "_frame%d.jpg" % count;count+=1 cv2.imwrite(filename, frame) cap.release() # reading all the frames from temp folder images = glob("E:/FCI Bio/GP/Dataset/Paper/3/3.1/OHP/Labeled_Dataset/videos/dataset/test/temp/*.jpg") prediction_images = [] for i in range(len(images)): img = image.load_img(images[i], target_size=(224,224,3)) img = image.img_to_array(img) img = img/255 prediction_images.append(img) # converting all the frames for a test video into numpy array prediction_images = np.array(prediction_images) # extracting features using pre-trained model prediction_images = base_model.predict(prediction_images) # converting features in one dimensional array prediction_images = prediction_images.reshape(prediction_images.shape[0], 7*7*512) # predicting tags for each array prediction = model.predict(prediction_images) # appending the mode of predictions in predict list to assign the tag to the video predict.append(y.columns.values[s.mode(prediction)[0][0]]) # appending the actual tag of the video actual.append(os.path.basename(videoFile).split('_')[0])
错误栈:
IndexError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_4364\3325358529.py in <module> 44 prediction = model.predict(prediction_images) 45 # appending the mode of predictions in predict list to assign the tag to the video ---> 46 predict.append(y.columns.values[s.mode(prediction)[0][0]]) 47 # appending the actual tag of the video 48 actual.append(os.path.basename(videoFile).split('_')[0]) IndexError: arrays used as indices must be of integer (or boolean) type
错误原因
错误出在第46行predict.append(y.columns.values[s.mode(prediction)[0][0]]),核心问题:
model.predict(prediction_images)返回的是浮点数类型的概率分布数组(每个元素对应标签的预测概率),不是整数标签索引- 直接对概率数组取众数无意义:浮点数概率几乎不会完全重复,且众数结果仍是浮点数,无法作为索引去访问
y.columns.values(索引必须是整数或布尔类型)
修复方案
先将每帧的概率数组转换为整数标签索引,再对这些索引取众数,最后用索引获取标签名称:
修改后的代码片段
替换原代码中从prediction = model.predict(prediction_images)到predict.append(...)的部分:
# predicting tags for each array (返回概率分布) prediction = model.predict(prediction_images) # 将每帧的概率转换为整数标签索引(取概率最大的标签) frame_labels = np.argmax(prediction, axis=1) # 对所有帧的标签索引取众数 video_label_idx = s.mode(frame_labels)[0][0] # 根据索引获取对应的标签名称并加入列表 predict.append(y.columns.values[video_label_idx]) # appending the actual tag of the video actual.append(os.path.basename(videoFile).split('_')[0])
关键说明
np.argmax(prediction, axis=1):对每个样本的概率数组,返回概率最大的标签的索引(整数类型),解决了索引类型不匹配的问题- 对整数类型的标签索引取众数,才是真正意义上的“多数帧预测的标签”,符合视频标签的逻辑
内容的提问来源于stack exchange,提问作者Mustafa Barkat
相关产品推荐
相关产品推荐

