ImportError:无法导入名称“ transpose_shape”

时间:2018-11-15 22:32:49

标签: python tensorflow keras anaconda

我正在与Andrew Ng一起学习Coursera深度学习课程,以使用YOLO算法进行对象检测,我正在尝试使用Windows和Anaconda Navigator在PC上运行该算法,我安装了Keras在TensorFlow后端上运行。但是当我键入以下内容时:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

LocationManager locationManager;

LocationListener locationListener;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 1) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            LatLng userlocation = new LatLng(location.getLatitude(), location.getLongitude());

            mMap.clear();
            mMap.addMarker(new MarkerOptions().position(userlocation).title("Your location)"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(userlocation));
            final CircleOptions circleOptions = new CircleOptions().radius(10000).center(new LatLng(location.getLatitude(), location.getLongitude()));
            final Circle circle = mMap.addCircle(circleOptions);
            Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
            try {
                List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

                if (listAddresses != null && listAddresses.size() > 0) {
                    Log.i("PlaceInfo", listAddresses.get(0).toString());

                }

            } catch (IOException e) {
                e.printStackTrace();

            }


        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    if (Build.VERSION.SDK_INT < 23) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }else{
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }else{
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0 ,0, locationListener);

            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            LatLng userlocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
            mMap.clear();
            mMap.addMarker(new MarkerOptions().position(userlocation).title("Your location)"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(userlocation));

    }
    }
}
}

它向我显示了以下内容:

import tensorflow as tf
from keras import backend as K

tensorflow_backend.py文件中的这一行似乎有问题:

Using TensorFlow backend.
Traceback (most recent call last):

  File "<ipython-input-8-0bcaeab92875>", line 2, in <module>
    from keras import backend as K

  File "C:\Users\AbanoubMamdouh\Anaconda3\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils

  File "C:\Users\AbanoubMamdouh\Anaconda3\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
    from . import conv_utils

  File "C:\Users\AbanoubMamdouh\Anaconda3\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
    from .. import backend as K

  File "C:\Users\AbanoubMamdouh\Anaconda3\lib\site-packages\keras\backend\__init__.py", line 89, in <module>
    from .tensorflow_backend import *

  File "C:\Users\AbanoubMamdouh\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 24, in <module>
    from ..utils.generic_utils import transpose_shape

ImportError: cannot import name 'transpose_shape'

我们将不胜感激! 干杯

2 个答案:

答案 0 :(得分:1)

可能是安装包配置错误:

pip uninstall -y tensorflow keras tf-nightly keras-nightly

然后:

pip install tensorflow

答案 1 :(得分:0)

尝试卸载 tensorflow 和 keras 并使用 pip 安装 keras,它也会安装 tensorflow。它对我有用!!!