我正在尝试针对图像分类的Intepretable AI解决方案。我们的工作基于Smooth Grad CAM plus plus。在我将pytorch安装到我的环境中之前,我有一个完美的代码。我一直在与keras库合作。我正在将图像传递给分类器,并试图可视化图层。
仅当在与我的代码相同的环境中安装pytorch后,我才出现错误。现在即使我已经创建了具有不同tensorflow版本的不同环境,也无法消除该错误。
这是错误的完整堆栈:
InvalidArgumentError Traceback (most recent call last)
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1658 try:
-> 1659 c_op = c_api.TF_FinishOperation(op_desc)
1660 except errors.InvalidArgumentError as e:
InvalidArgumentError: slice index 7231 of dimension 1 out of bounds. for 'strided_slice_5' (op: 'StridedSlice') with input shapes: [?,1000], [2], [2], [2] and with computed input tensors: input[1] = <0 7231>, input[2] = <1 7232>, input[3] = <1 1>.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
in
10 gradCAM_VGG16 = cam(GradCAM,base_model,target_layer,X)
11 gradCAMPP_VGG16 = cam(GradCAMPlusPlus,base_model,target_layer,X)
---> 12 smoothgradCAMPP_VGG16 = cam(SmoothGradCAMPlusPlus, base_model, target_layer, X, 10, 0.15)
13 print("Guided CAMs for ",decoded_y[0]," using ",base_model)
14
in cam(classname, model, target_layer, image, n_samples, stdev_spread)
1 def cam(classname,model,target_layer,image,n_samples=25, stdev_spread=0.15):
2 class_object = classname(model,target_layer=target_layer)
----> 3 cam = class_object(image)
4
5 return cam
in call(self, x)
18
19 def call(self, x):
---> 20 return self.forward(x)
21
22 def forward(self, x):
in forward(self, x)
29
30 prediction = np.argmax(self.model.predict(x))
---> 31 cam = self.getSmoothGradCAMPP(prediction,x)
32
33 return cam
in getSmoothGradCAMPP(self, prediction, x)
47 #x_with_noise = GaussianNoise(np.array(x))
48 x_with_noise = K.random_normal(shape = (x.shape), mean=x,stddev=std_tensor)
---> 49 smg_cam = cam(GradCAMPlusPlus,self.model,self.target_layer,x_with_noise)
50
51 if i == 0:
in cam(classname, model, target_layer, image, n_samples, stdev_spread)
1 def cam(classname,model,target_layer,image,n_samples=25, stdev_spread=0.15):
2 class_object = classname(model,target_layer=target_layer)
----> 3 cam = class_object(image)
4
5 return cam
in call(self, x)
35
36 def call(self, x):
---> 37 return self.forward(x)
38
39 def getGradCAMPlusPlus(self, prediction,image):
in forward(self, x, idx)
30 """
31 prediction = np.argmax(self.model.predict(x,steps=16))
---> 32 cam = self.getGradCAMPlusPlus(prediction,x)
33
34 return cam
in getGradCAMPlusPlus(self, prediction, image)
48 cam: class activation map. shape=> (1, 1, H, W)
49 '''
---> 50 class_score = self.model.output[0, prediction]
51 conv_layer_output = self.model.get_layer(self.target_layer).output
52 grads = K.gradients(class_score, conv_layer_output)[0]
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\ops\array_ops.py in _slice_helper(tensor, slice_spec, var)
652 ellipsis_mask=ellipsis_mask,
653 var=var,
--> 654 name=name)
655
656
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\ops\array_ops.py in strided_slice(input_, begin, end, strides, begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask, var, name)
818 ellipsis_mask=ellipsis_mask,
819 new_axis_mask=new_axis_mask,
--> 820 shrink_axis_mask=shrink_axis_mask)
821
822 parent_name = name
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\ops\gen_array_ops.py in strided_slice(input, begin, end, strides, begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask, name)
11364 ellipsis_mask=ellipsis_mask,
11365 new_axis_mask=new_axis_mask,
11366 shrink_axis_mask=shrink_axis_mask, name=name)
11367 _result = _op.outputs[:]
11368 _inputs_flat = _op.inputs
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
786 op = g.create_op(op_type_name, inputs, output_types, name=scope,
787 input_types=input_types, attrs=attr_protos,
--> 788 op_def=op_def)
789 return output_structure, op_def.is_stateful, op
790
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs)
505 'in a future version' if date is None else ('after %s' % date),
506 instructions)
--> 507 return func(*args, **kwargs)
508
509 doc = _add_deprecated_arg_notice_to_docstring(
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\framework\ops.py in create_op(failed resolving arguments)
3298 input_types=input_types,
3299 original_op=self._default_original_op,
-> 3300 op_def=op_def)
3301 self._create_op_helper(ret, compute_device=compute_device)
3302 return ret
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\framework\ops.py in init(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1821 op_def, inputs, node_def.attr)
1822 self._c_op = _create_c_op(self._graph, node_def, grouped_inputs,
-> 1823 control_input_ops)
1824
1825 # Initialize self._outputs.
C:\Anaconda\envs\python3.6.5\lib\site-packages\tensorflow\python\framework\ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
1660 except errors.InvalidArgumentError as e:
1661 # Convert to ValueError for backwards compatibility.
-> 1662 raise ValueError(str(e))
1663
1664 return c_op
ValueError: slice index 7231 of dimension 1 out of bounds. for 'strided_slice_5' (op: 'StridedSlice') with input shapes: [?,1000], [2], [2], [2] and with computed input tensors: input[1] = <0 7231>, input[2] = <1 7232>, input[3] = <1 1>.