AttributeError:“模块”对象没有属性“ CUDA”

时间:2018-11-03 05:19:43

标签: keras theano theano-cuda

我正在尝试运行此存储库:https://github.com/WaqasSultani/AnomalyDetectionCVPR2018  在Test_Anomaly_Detector_public.py中,我陷入了错误:theano.sandbox.cuda.use('gpu0') AttributeError:'模块'对象没有属性'cuda'。 我正在使用theano作为后端

这是Test_Anomaly_Detector_public.py:

#include <stdio.h>
#include<stdlib.h>
#define MAX 5

typedef struct stack {
  int top;
  int arr[MAX];
} stack;

void enque(stack*s1, int ele) {
  printf("entering");
  push(&s1, ele);
  printf("what a pain");
}

void push(stack*s, int ele) {
  if (s->top == MAX - 1) {
    printf("OVERFLOW");
  } else {
    s->arr[++s->top] = ele;
  }
}

int deq(stack*s1, stack*s2) {
  int x;
  if (s1->top == -1 && s2->top == -1) {
    printf("empty");
  } else {
    if (s2->top == -1) {
      while (s1->top != -1) {
        push(&s2, pop(&s1));
      }
    }
    x = pop(&s2);
    return x;
  }
}

int pop(stack *s) {
  if (s->top == -1) {
    printf("UNDERFLOW");
  } else {
    return s->arr[s->top--];
  }
}

void display(stack*s) {
  printf("entered display");
  int i;
  for (i = 0; i <= s->top; i++) {
    printf(" %d", s->arr[i]);
  }
}

int main() {
  int ch, ele, c;
  stack s1, s2;
  s1.top = -1, s2.top = -1;
  do {
    printf("1 - Enqueue2-deq3-display4-exit\n");
    printf("Enter choice");
    scanf("%d", &ch);
    switch (ch) {
      case 1:
        printf("enter ele of ur choice");
        scanf("%d", &ele);
        enque(&s1, ele);
        break;
      case 2:
        c = deq(&s1, &s2);
        printf("%d", c);
        break;
      case 3:
        display(&s1);
        break;
      case 4:
        exit(0);
      default:
        printf("Wrong choice");
    }
  } while (ch != 5);
}

我的.theanorc文件:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.regularizers import l2
from keras.optimizers import SGD ,Adagrad
from scipy.io import loadmat, savemat
from keras.models import model_from_json
import theano.tensor as T
import theano
import csv
import ConfigParser
import collections
import time
import csv
import os
from os import listdir
import skimage.transform
from skimage import color
from os.path import isfile, join
import numpy as np
import numpy
from datetime import datetime
from scipy.spatial.distance import cdist,pdist,squareform
import theano.sandbox
import shutil
theano.sandbox.cuda.use('gpu0')




seed = 7
numpy.random.seed(seed)


def load_model(json_path):  # Function to load the model
    model = model_from_json(open(json_path).read())
    return model

def load_weights(model, weight_path):  # Function to load the model weights
    dict2 = loadmat(weight_path)
    dict = conv_dict(dict2)
    i = 0
    for layer in model.layers:
        weights = dict[str(i)]
        layer.set_weights(weights)
        i += 1
    return model

def conv_dict(dict2):
    i = 0
    dict = {}
    for i in range(len(dict2)):
        if str(i) in dict2:
            if dict2[str(i)].shape == (0, 0):
                dict[str(i)] = dict2[str(i)]
            else:
                weights = dict2[str(i)][0]
                weights2 = []
                for weight in weights:
                    if weight.shape in [(1, x) for x in range(0, 5000)]:
                        weights2.append(weight[0])
                    else:
                        weights2.append(weight)
                dict[str(i)] = weights2
    return dict

# Load Video

def load_dataset_One_Video_Features(Test_Video_Path):

    VideoPath =Test_Video_Path
    f = open(VideoPath, "r")
    words = f.read().split()
    num_feat = len(words) / 4096
    # Number of features per video to be loaded. In our case num_feat=32, as we divide the video into 32 segments. Note that
    # we have already computed C3D features for the whole video and divided the video features into 32 segments.

    count = -1;
    VideoFeatues = []
    for feat in xrange(0, num_feat):
        feat_row1 = np.float32(words[feat * 4096:feat * 4096 + 4096])
        count = count + 1
        if count == 0:
            VideoFeatues = feat_row1
        if count > 0:
            VideoFeatues = np.vstack((VideoFeatues, feat_row1))
    AllFeatures = VideoFeatues

    return  AllFeatures



print("Starting testing...")


AllTest_Video_Path = '/newdata/UCF_Anomaly_Dataset/Dataset/CVPR_Data/C3D_Complete_Video_txt/Test/'
# AllTest_Video_Path contains C3D features (txt file)  of each video. Each file contains 32 features, each of 4096 dimensions.
Results_Path = '../Eval_Res/'
# Results_Path is the folder where you can save your results
Model_dir='../Trained_AnomalyModel/'
# Model_dir is the folder where we have placed our trained weights
weights_path = Model_dir + 'weights_L1L2.mat'
# weights_path is Trained model weights

model_path = Model_dir + 'model.json'

if not os.path.exists(Results_Path):
       os.makedirs(Results_Path)

All_Test_files= listdir(AllTest_Video_Path)
All_Test_files.sort()

model=load_model(model_path)
load_weights(model, weights_path)
nVideos=len(All_Test_files)
time_before = datetime.now()

for iv in range(nVideos):

    Test_Video_Path = os.path.join(AllTest_Video_Path, All_Test_files[iv])
    inputs=load_dataset_One_Video_Features(Test_Video_Path) # 32 segments features for one testing video
    predictions = model.predict_on_batch(inputs)   # Get anomaly prediction for each of 32 video segments.
    aa=All_Test_files[iv]
    aa=aa[0:-4]
    A_predictions_path = Results_Path + aa + '.mat'  # Save array of 1*32, containing anomaly score for each segment. Please see Evaluate Anomaly Detector to compute  ROC.
    print "Total Time took: " + str(datetime.now() - time_before)

1 个答案:

答案 0 :(得分:0)

您可以注释掉这一行。跑步时,请遵循此

THEANO_FLAGS=mode=FAST_RUN,device=cuda0,floatX=float32 python [...]